Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Establish what position the switch should be in
- if([info.state isEqualToString:@"on"]){
- mySwitch.selected = TRUE;
- } else {
- mySwitch.selected = FALSE;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
- }
- // Configure the cell...
- [self configureCell:cell atIndexPath:indexPath];
- return cell;
- }
- - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
- InfoObject *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
- cell.textLabel.text = info.name;
- // Use a custom UISwitch that holds a pointer to the row it is associated with
- NamedUISwitch *mySwitch = [[[NamedUISwitch alloc] initWithFrame:CGRectZero] autorelease];
- mySwitch.indexPath = indexPath;
- [mySwitch addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];
- // And of course we need to know what the state is
- NSLog(@"switchState:%@",info.state);
- // Establish what position the switch should be in
- if([info.state isEqualToString:@"on"]){
- mySwitch.selected = TRUE;
- } else {
- mySwitch.selected = FALSE;
- }
- cell.accessoryView = mySwitch;
- }
- - (void) changeState:(id)sender {
- InfoObject *info = [self.fetchedResultsController objectAtIndexPath:((NamedUISwitch*)sender).indexPath];
- if(((NamedUISwitch*)sender).on){
- info.state = @"on";
- } else {
- info.state = @"off";
- }
- NSError *error;
- if (![self.context save:&error]) {
- NSLog(@"Whoops, couldn't save state: %@", [error localizedDescription]);
- //TODO: alertview
- }
- }
- // Establish what position the switch should be in
- if([info.state isEqualToString:@"on"]){
- mySwitch.on = YES;
- } else {
- mySwitch.on = NO;
- }
Add Comment
Please, Sign In to add comment