I am trying to put a UISwitch
into a UITableView
. I have looked at this question here and here. Since the table view reuses cells I need to save the state of the cells. I achieve this by having a NSMutableDictionary
where the row of the cell maps the state (true or false) of the UISwitch. The problem I am running into is identifying the index of the UISwitch in the TableView. I think something is wrong with the @selector statement. This has been driving me crazy for the past 2 hours. Is this the right way to do it and why do I change the state of any of the UISwitch it always reports with the same log message (see below). Any help is greatly appreciated. Here is some snippets of my code ....
cellForRowAtIndexPath
//Need to construct
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch *temp = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
//Add a callback when the value is changed
[temp addTarget:self action:@selector(switchButtonChanged:forEvent:) forControlEvents:UIControlEventValueChanged];
//Set the acessory view to the UISwitch
cell.accessoryView = temp;
}
Callback Method
- (void)switchButtonChanged:(id)sender forEvent:(UIEvent *)event
{
UISwitch *tempSwitch = (UISwitch *) sender;
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];
NSNumber *isChecked = [[[NSNumber alloc] initWithBool:tempSwitch.on] autorelease];
NSNumber *row = [[[NSNumber alloc] initWithInteger:indexPath.row] autorelease];
[self.selectedItems setObject:isChecked forKey:row];
NSLog(@"Row %@ is now %@", row, [self.selectedItems objectForKey:row]);
}
Log
Row 0 is now (null)
Row 0 is now (null)
Row 0 is now (null)