Ho un UITableView
con UISwitchs
su di essi.Selezionare la riga di UITableView quando si fa clic su UISwitch
Quando l'interruttore viene commutato Voglio correre una funzione. La funzione registra solo se l'interruttore è acceso o spento e la riga su cui è stato cambiato l'interruttore. Il problema che sto avendo è che quando clicco sull'interruttore non registra la riga corretta a meno che non abbia fatto clic su quella riga prima di fare clic sull'interruttore.
Credo che il mio problema è che facendo clic sull'interruttore non si seleziona la riga. Come posso fare in modo che selezioni la riga o posso aggiungere l'ID all'interruttore?
Quindi l'ID dell'interruttore "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");
}
In iOS7, la superview sarà UITableViewCellContentView, che è racchiusa in UITableViewCellScrollView. È possibile invece ottenere il percorso dell'indice della cella utilizzando quanto segue: CGPoint switchPositionPoint = [sender convertPoint: CGPointZero toView: [self tableView]]; NSIndexPath * indexPath = [[self tableView] indexPathForRowAtPoint: switchPositionPoint]; – NSDestr0yer