I have a UITableView
with UISwitchs
on them.
When the switch is toggled I want to run a function. The function just logs If the switch is on or off and the row that the switch has been changed on. The problem that im having is that when I click on the switch it does not log the correct row unless I have clicked on that row before clicking the switch.
I guess my problem is that clicking the switch does not select the row. How can I make it so that it either selects the row or can I add the ID to the switch?
So switch ID "1" is "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" );
}
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" );
}
You should set the IndexPath.row
as a Tag to each Switch
in cellForRowAtIndexPath
Method
switchView.tag= indexPath.row;
And when switch value change .you'll get the Row number
- (void) switchChanged:(UISwitch *)sender {
int rowIndex =[sender tag];
//rowIndex you may use it further as you wanted.
}
You can retrieve the NSIndexPath for the UISwitch that was changed in the tableview. This is the same idea for any control as already answered in this post : Detecting which UIButton was pressed in a UITableView
- (void) switchChanged:(id)sender
{
CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]];
NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint];
}
This will work for iOS7, previously I used [sender superview] but that now returns a UITableViewCellContentView inside of a UITableViewCellScrollView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With