Detect tap on a button in UITableViewCell for UITableView containing multiple sections

前端 未结 4 1852
误落风尘
误落风尘 2020-12-04 16:00

I want to detect button tap on a UITableViewCell where the parent UITableView consists of multiple sections.

I was able to do it in the case of single section, but I

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 16:38

    A more direct way to get the index path without using CGPoint and making assumptions about UI is to look at the .superview. I'd imagine this is more reliable in practice.

    - (myTableViewCell *)cellContainingView:(UIView *)view
    {
        do {
            view = view.superview;
        } while (view != nil && ![view isKindOfClass:[myTableViewCell class]]);
    
        return (myTableViewCell *)view;
    }
    

    Then you can simply do this in the button action:

    - (IBAction)myButtonPressed:(id)sender
    {
        myTableViewCell *cell = [self cellContainingView:(UIView *)sender];
        NSIndexPath *path = [self.tableView indexPathForCell:cell];
    }
    

提交回复
热议问题