UITableViewCell上的按钮在编辑模式下不起作用

时间:2016-01-21 16:05:24

标签: ios objective-c uitableview cocoa-touch uikit

我有一个UITableView填充了包含按钮的单元格。我希望这些按钮在编辑模式下也能正常工作,但它们并不适用。似乎UITableViewCell上的手势识别器阻止按钮上的手势识别器。有没有人有任何关于解决这个问题的建议?

ViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.items = [@[@"one", @"two", @"three", @"four"] mutableCopy];
}
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cellID" 
                                                          forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[TableViewCell alloc] init];
    }

    [cell.button setTitle: self.items[indexPath.row] 
                 forState: UIControlStateNormal];
    return cell;
}

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
     return YES;
 }

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.items removeObjectAtIndex: indexPath.row];
    }
}

TableViewCell.h:

@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)buttonTapped:(id)sender;
@end

因此,当一个单元格没有处于编辑模式时调用buttonTapped:,但是当一个单元格处于编辑模式时,该按钮不起作用。

3 个答案:

答案 0 :(得分:0)

使用单元格中每个按钮的不同标记。例如,

cellforRowAtIndexPath 内,指定按钮的标记。

[Button addTarget:self action:@selector(func:event:) forControlEvents:UIControlEventTouchUpInside];
[Button setTag:indexPath.row];

现在调用该函数并使用标记引用单元格。

-(IBAction)func:(id)sender event:(id)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint Pos = [touch locationInView:self.tableview];
    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:Pos];
    if(indexPath != nil){
        //do operation with indexPath
    }
 }

希望它有所帮助...

答案 1 :(得分:0)

首先看一些代码你做了什么!!没有适当的声明,很难给出答案。 首先,我的建议是在UItableview委托方法中给出手势识别。

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

// code

}

有时手势不能解决编码中的小错误。

如果可能的话,

您可以将其添加到viewDidLoad中的UItableview,而不是直接将Gesture识别器添加到Cell。 这是在UITableview中处理手势的最佳方式。

如示例中的Phix所述,

在didSwipe-Method中,您可以按如下方式确定受影响的IndexPath和单元格:

-(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];
        // ...
  }
}

您可以设置其他手势,如上方。 我希望它能解决你的问题。

答案 2 :(得分:-1)

为此,您需要继承UITableView并使其符合UIGestureRecognizerDelegate协议。在UITableView子类的.m文件中添加以下代码:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass: [UIButton class]]) {
        return NO;
    }
    return YES;
}