2011-08-14 3 views
0

각 행이 코어 데이터의 개체를 나타내는 테이블이 있습니다. 각 행에는 코어 데이터에서 객체의 상태 속성을 토글해야 할 때 UISwitch (UISwitch가 포함 된 행의 indexPath를 포함하도록 수정 됨)가 포함되어 있습니다.코어 데이터 상태가 변경되면 UISwitch가 켜지지 않습니다.

현재 스위치가 켜져 있고 스위치가 켜지면 다시 켜집니다!?

내가 가진 하나의 단서는 내가

// Establish what position the switch should be in 
    if([info.state isEqualToString:@"on"]){ 
     mySwitch.selected = TRUE; 
    } else { 
     mySwitch.selected = FALSE; 
    } 

과 changeState 방법에서 두 info.state 줄을 주석 때, 스위치가 잘 작동한다는 것입니다. info.state setter는 스위치 상태에 관계없이 스위치가 전환되지 않도록합니다.

나는 방대하게 과실이며 메모리를 올바르게 관리하지 않습니까?

- (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 
    } 
} 

답변

4

는 그 코드가 있어야한다고 생각 :

// Establish what position the switch should be in 
    if([info.state isEqualToString:@"on"]){ 
     mySwitch.on = YES; 
    } else { 
     mySwitch.on = NO; 
    } 
+0

하는 것은 완벽하게 일했다. UISwitch의 모든 속성이 나를 혼란스럽게 만들었습니다! changeState 메서드에서 on 속성을 사용했지만 이전 버전을 변경하는 것을 잊었습니다. 감사. – nflacco

관련 문제