سؤال

أنا أرفق أ UISwipeGestureRecognizer إلى UITableViewCell في ال cellForRowAtIndexPath: طريقة مثل ذلك:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
        gesture.direction = UISwipeGestureRecognizerDirectionRight;
        [cell.contentView addGestureRecognizer:gesture];
        [gesture release];
    }
    return cell;
}

ومع ذلك ، فإن didSwipe يتم استدعاء الطريقة دائمًا مرتين على انتقاد ناجح. لقد اعتقدت في البداية أن هذا كان لأن الإيماءة تبدأ وتنتهي ، لكن إذا قمت بتسجيل الخروج من GesturerEcognizer نفسه ، فإن كلاهما في حالة "النهاية":

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {

    NSLog(@"did swipe called %@", gestureRecognizer);
}

وحدة التحكم:

2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>

أنا حقا لا أعرف لماذا. حاولت بوضوح التحقق من الحالة النهائية ، لكن هذا ليس مساعدة لأن كلاهما يأتي كـ "انتهى" على أي حال ... أي أفكار؟

هل كانت مفيدة؟

المحلول

بدلاً من إضافة معرف الإيماءة إلى الخلية مباشرة ، يمكنك إضافتها إلى TableView في viewDidLoad.

في ال didSwipe-يمكنك تحديد الفهرس والخلية المتأثرة على النحو التالي:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {

  if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
        NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
        UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
        // ...
  }
}

نصائح أخرى

ستعمل مع App Epelegate

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{

// code

}

لقد واجهت نفس المشكلة وحلها عن طريق تحديد "التمرير الممكّن" في سمات عرض الجدول.

لا يحتاج عرض الجدول الخاص بي إلى التمرير ، لذلك لا يؤثر على التطبيق بأي طريقة أخرى ، إلا أنني الآن لا أحصل على أول نقرة لا تستجيب بعد لفتة التمرير.

إضافة لفتة في طريقة oakefromnib تعمل دون أي مشاكل.

class TestCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        let panGesture = UIPanGestureRecognizer(target: self,
                                            action: #selector(gestureAction))
        addGestureRecognizer(panGesture)
    }

    @objc func gestureAction() {
        print("gesture action")
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top