2013-05-08 2 views
0

질문을 게시하기 전에 본인은 본때를 보여주는 앱임을 명시해야합니다. 이것이 필자가 파일 시스템의 "기괴한"폴더에 쓰는 이유입니다.didSelectRowAtIndexPath에서 액세서리 유형을 변경할 수 없습니까?

계속하겠습니다. 당신이 볼 수처럼

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"pluginCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 
    if(indexPath.row == 0) 
    { 
     cell.textLabel.text = @"default"; 
    }else 
    { 
     //Get the plugin's display name. 
     NSBundle *currentPlugin = [[NSBundle alloc] initWithPath:[NSString stringWithFormat:@"/Library/Cydeswitch/plugins/%@", [plugins objectAtIndex:indexPath.row - 1], nil]]; 
     cell.textLabel.text = [[currentPlugin localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"]; 
     if(cell.textLabel.text == nil) 
     { 
      //No localized bundle, so let's get the global display name... 
      cell.textLabel.text = [[currentPlugin infoDictionary] objectForKey:@"CFBundleDisplayName"]; 
     } 
     [currentPlugin release]; 
    } 

    if([[[cell textLabel] text] isEqualToString:[settings objectForKey:@"pluginToExecute"]]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     currentCell = [cell retain]; 
    } 
    return cell; 
} 

는,이 방법은 현재 "선택"하는 세포를 가리 키도록 currentCell라는 멤버를 사용

여기 내 cellForRowAtIndexPath 방법입니다. 이것은 옵션 테이블이며 사용자는 언제든지 Checkmark 액세서리 아이콘이있는 셀을 하나만 가질 수 있어야합니다.

다른 셀을 사용할 때 옵션을 변경하면 현재 셀에서 체크 표시가 사라지고 새로 나타난 셀에 나타납니다. 나는 이것을 이렇게한다 :

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    currentCell.accessoryType = UITableViewCellAccessoryNone; 
    [currentCell release]; 
    currentCell = [[self tableView:tableView cellForRowAtIndexPath:indexPath] retain]; 
    NSLog(@"CURRENT CELL %@", currentCell.textLabel.text); 
    currentCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

그러나 그것은 작동하지 않는다. 다른 셀을 탭하는 순간, Checkmark는 이전 셀에서 올바르게 사라지지만 새로운 셀에는 나타나지 않습니다.

NSLog가 새 셀의 텍스트를 잘 인쇄하기 때문에 잘 작동합니다.

이전에 indexPath를 추적하려고 시도했지만 전혀 작동하지 않았습니다. 셀에 대한 포인터 대신 indexPaths를 사용하여 시도했을 때 사용자가 아무 것도하지 않았을 때 (적어도 현재 접근 방식으로는 이전 셀에서 체크 표시가 사라짐).

나는 cellForRowAtIndexPath와 관련이 있다고 생각한다. 왜냐하면 셀을 가리키고 있으면 체크 표시가 사라지기 때문에 cellForRowAtIndexPath로 가져온 셀에서 액세서리 유형을 변경하려고 할 때 어떤 이유로 작동하지 않는 것 같다. 모든.

도움이 될 것입니다.

답변

2

오타? 이것을 시도하십시오 :

currentCell = [[self.tableView cellForRowAtIndexPath:indexPath] retain]; 
+0

네,이 솔루션은 하하 위의 대답과 함께 작동합니다. 잡종 대답. –

1

마지막으로 선택한 셀을 자신의 방식대로 추적해서는 안됩니다. 세포가 재사용됩니다. ivar을 사용하여 indexPath 또는 데이터에 적합한 다른 키를 추적하십시오.

그런 다음 didSelect... 메서드에서 저장된 indexPath 또는 키를 사용하여 이전 셀에 대한 참조를 얻습니다. cellForRow... 메서드에서 현재 indexPath가 저장된 indexPath와 일치하는지 여부에 따라 적절한 accessoryType을 설정해야합니다.

마지막으로 대리인/데이터 원본 메서드를 호출하지 마십시오. 셀에 대한 참조를 가져올 때 직접 테이블보기에 요청하십시오.

현재 - 귀하의 cellForRow... 방법에서 currentCell을 과도하게 보유하고 있습니다. 과제를 처음 작성하는 경우가 아니면 그 방법으로 모든 것을 유지할 필요가 없습니다.

+0

감사합니다. 솔루션은 위의 대답에서 구문과 함께 생각합니다. 나는 실제로 마지막 인덱스 경로를 추적하고 있었으므로 그 결과는 같았다. 나는 노력할 것이다. 감사. –