Select UITableView's row when clicking on UISwitch

后端 未结 7 851
眼角桃花
眼角桃花 2020-12-06 07:45

I have a UITableView with UISwitchs on them.

\"TableView\"

When the switch is

7条回答
  •  爱一瞬间的悲伤
    2020-12-06 08:39

    To find the cell that holds the switch

    UISwitch *switchInCell = (UISwitch *)sender;
    UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
    

    To find the indexpath of that cell

    NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
    

    In your case

     - (void) switchChanged:(id)sender {
    
             UISwitch *switchInCell = (UISwitch *)sender;
             UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
             NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
             NSString *strCatID =[[NSString alloc]init];
             strCatID = [self.catIDs objectAtIndex:indexpath];
             NSLog( @"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF" );
            }
    

提交回复
热议问题