How do I retrieve UITableView row number of a UISwitch?

前端 未结 6 1114
你的背包
你的背包 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:08

    Similar to @danh, I've come up with this solution using an extention which I've used multiple times.

    @interface UIView (Find)
    - (id)findSuperviewOfClass:(Class)class;
    - (NSIndexPath *)findIndexPath;
    @end
    
    @implementation UIView (Find)
    - (id)findSuperviewOfClass:(Class)class
    {
        return [self isKindOfClass:class] ? self : [self.superview findSuperviewOfClass:class];
    }
    
    - (NSIndexPath *)findIndexPath
    {
        UITableView *tableView = [self findSuperviewOfClass:[UITableView class]];
        return [tableView indexPathForCell:[self findSuperviewOfClass:[UITableViewCell class]]];
    }
    @end
    

提交回复
热议问题