【问题标题】:Buttons on UITableViewCell not working when in editing modeUITableViewCell 上的按钮在编辑模式下不起作用
【发布时间】:2016-01-21 16:05:23
【问题描述】:

我有一个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: 在单元格不处于编辑模式时被调用,但当单元格处于编辑模式时,按钮不起作用。

【问题讨论】:

  • 您可能希望显示您使用的代码,1- 如何添加按钮,2- 如何启用“编辑”模式,3- 您使用的任何手势识别器。
  • 我指的是 UIKit 对手势识别器的使用。故事板中添加了按钮

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


【解决方案1】:

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

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
    }
 }

希望对你有帮助...

【讨论】:

    【解决方案2】:

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

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

    有时由于编码中的小错误,手势无法正常工作。

    如果可能的话,其次,

    除了直接将Gesture识别器添加到Cell中,您可以将其添加到viewDidLoad中的UItableview中。 这是在 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];
            // ...
      }
    }
    

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

    【讨论】:

    • 抱歉,我的问题表述得不好。我更新了它,所以请检查您现在是否看到任何解决方案。无论如何感谢您的帮助
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多