How do I retrieve UITableView row number of a UISwitch?

前端 未结 6 1111
你的背包
你的背包 2021-01-13 06:18

I have tried several approaches posted here, but I cannot get my table full of switches to return an index value for the cell of the changed switch. I am creating the view c

6条回答
  •  北海茫月
    2021-01-13 07:01

    Tags is an okay solution, but a little clumsy because the cells - and therefore their subviews - are continually being reused, changing their rows - and therefore the tags they need.

    Instead, I generally keep one of these around:

    - (NSIndexPath *)indexPathWithSubview:(UIView *)subview {
    
        while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
            subview = subview.superview;
        }
        return [self.tableView indexPathForCell:(UITableViewCell *)subview];
    }
    

    Then when I get an IBAction:

    - (IBAction)someSubviewAction:(id)sender {
    
        NSIndexPath *indexPath = [self indexPathWithSubview:(UIView *)sender];
        // carry on from here
    }
    

提交回复
热议问题