iOS:在tableViewCell底部调用的willSelectRowAtIndexPath

问题描述

|| 我有一个自定义UITableViewCell,上面有2个按钮和一个标签。问题是,仅当我在单元格底部上方单击时,才会调用willSelectRowAtIndexPath。 我将UITableViewCell的“用户交互启用”设置为true。这使我在单元格中具有的按钮可以单击。 如果将其设置为false,则单击单元格中的任何位置时,都会调用willSelectRowAtIndexPath,但按钮将不再起作用。 请帮忙!     

解决方法

我不确定启用该按钮后是否可以自动执行该操作。但是,我们可以让单元格知道它已被选中。
- (IBAction)respondToClick:(id)sender {
    CustomTableViewCell *customCell = (CustomTableViewCell*)sender.superview;

    // You can signal the cell to select itself (will not send the delegate message)
    [customCell setSelected:YES animated:NO];

    // Alternately this can be done but I don\'t recommend
    NSIndexPath *indexPath = [myTableView indexPathForCell:customCell];
    if ( myTableView.delegate && [myTableView.delegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)] ) {
        [myTableView.delegate tableView:myTableView willSelectRowAtIndexPath:indexPath];
    }
}