我对它们有UITableView
和UISwitchs
。当点击UISwitch时选择UITableView的行
当开关处于我想运行的功能。该功能只记录开关是打开还是关闭,以及开关已打开的行。 im的问题在于,当我点击开关时,它不会记录正确的行,除非在点击开关之前点击了该行。
我想我的问题是,单击开关不会选择该行。我怎样才能做到这一点,以便它选择行或我可以将ID添加到交换机?
所以开关ID“1”是“ON”。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"POICell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//set the cell text to the catName
cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
//add switch
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
// Configure the cell...
return cell;
}
- (void) switchChanged:(id)sender {
NSString *StrCatID =[[NSString alloc]init];
StrCatID = [self.catIDs objectAtIndex:[self.inputTableView indexPathForSelectedRow].row];
UISwitch* switchControl = sender;
NSLog(@"The switch for item %@ is %@",StrCatID, switchControl.on ? @"ON" : @"OFF");
}
在iOS7中,superview将是一个UITableViewCellContentView,它被包装在UITableViewCellScrollView中。您可以通过使用以下代码获取单元的索引路径:CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]]; NSIndexPath * indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint]; – NSDestr0yer