2011-11-14 2 views
0

섹션 테이블 뷰를 사용하여 사용자가 테이블에서 항목 하나를 선택하도록합니다. 사용자가 항목을 선택하면 항목 옆에 확인 표시가 나타납니다 (UITableViewCellAccessoryCheckmark 사용). 이전 선택을 한 경우 이전에 선택한 행에서 검사를 제거해야합니다.섹션 테이블에서 행에 대한 accessoryType 변경 사용자가 행을 선택한 후보기

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    int newRow = [indexPath row]; 

    int oldRow = [lastIndexPath row]; 

    if (newRow != oldRow || newRow == 0) 
    { 

     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     [lastIndexPath release]; 
     lastIndexPath = indexPath; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

lastIndexPath.h 파일에 개인 선언 : 여기에 내가 사용하고있는 코드입니다.

이 코드는 섹션으로 구분되지 않은 작은 목록에 유용합니다. 그러나 섹션으로 구분 된 대형 테이블에서는 다른 섹션의 행에 임의의 체크 표시가 나타납니다. cellForRowAtIndexPath이 indexPath의 섹션을 무시하는 것과 거의 같습니다.

최소 섹션의 행 수보다 큰 행을 선택하면 코드도 충돌합니다. 당신이 여기에서 발생하는

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSUInteger section = [indexPath section]; 

    NSUInteger row = [indexPath row]; 

    NSString *key = [keys objectAtIndex:section]; 

    NSArray *itemSection = [items objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease]; 

    } 
    NSArray *rowLabel = [itemSection objectAtIndex:row]; 

    cell.textLabel.text = [rowLabel objectAtIndex:1]; 

    NSString *detText = [rowLabel objectAtIndex:0]; 

    detText = [detText stringByAppendingString:@"   $"]; 

    detText = [detText stringByAppendingString:[rowLabel objectAtIndex:2]]; 

    cell.detailTextLabel.text = detText; 

    return cell; 
} 
+0

은 'cellForRowAtIndexPath :'에 대한 코드를 참조해야합니다. 나는 당신이 셀을 dequeuing하고 재사용 식별자 (cellForRowAtIndexPath :'의 기본 구현)를 사용하고 있다고 가정하고있다. 이 경우 셀을 작성하는 코드가 표시된 셀을 템플릿으로 재사용하여 새 셀을 작성합니다. 이 문제를 해결하려면 몇 가지 작업을 수행 할 수 있습니다. "매번 cellForRowAtIndexPath :'"또는 "각 셀에 대한 참조를 유지하고'cellForRowAtIndexPath :'에 대한 호출을 회피하십시오. – ColdLogic

+0

예 - 말하고 이것은 무작위 체크 표시의 첫 번째 문제를 해결할 수 있지만 가장 작은 섹션의 행 수보다 큰 행을 선택하면 충돌의 두 번째 문제가 해결되지 않을 것이라고 생각합니다. 여기에 cellForRowAtIndexPath 코드가 있습니다 : –

+0

당신은 코멘트에 그 코드를 게시 할 수 없을 것입니다. 원래 게시물에 추가하십시오. – ColdLogic

답변

0

하나의 문제는 당신이 lastIndexPath의 indexPath를 저장하는 방법은 다음과 같습니다 여기

은 cellForRowAtIndexPath 코드입니다. lastIndexPath에 저장하고있는 indexPath를 유지해야합니다. 이 메서드에 전달 된 indexPath는 자동으로 렌더링되므로 사용자가이 메서드를 유지하지 않으면 아래에서 벗어날 수 있습니다. 이것은 충돌을 일으킬 수 있습니다.

0

아마도 이것은 정확히 당신이 찾고있는 것입니다. 나는 내 애플 리케이션 중 하나를 위해 이것을 개발했습니다. 즐겨 ! (그렇다면 응답으로 표시하십시오.) 또한 ARC를 사용하므로 보존 및 해제 할 수 없습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    /* 
    1. First we get the indexPath from the prior priorSelectedRowInteger and priorSelectedSectionInteger ivars. Note: we could use a single indexPath ivar, but we separate them into row and section here for clarity. 
    2. Then we reset the selectedRowInteger ivar to the currently selected row. This must be done before any rows are reloaded. 
    3. Then we reload only the two rows at the concerned index paths, as we have captured the indexPath of the prior selected row and the method gives us the new one. We could just simply reload the table here with [self.tableView reloadData], but it would not be animated and not as smooth. 

    */ 
    NSIndexPath *priorSelectedIndexPath = [NSIndexPath indexPathForRow:priorSelectedRowInteger inSection: priorSelectedSectionInteger]; 
    // Now that we have the priorSelectedIndexPath, we save the new one for the next round. 
    self.priorSelectedRowInteger = indexPath.row; 
    self.priorSelectedSectionInteger = indexPath.section; 

    // For a changing tableView, check to make sure the priorIndexPath is still valid before trying to reload the prior row. 
    // NSLog(@"priorSelectedIndexPath %@", priorSelectedIndexPath); 
    if ((tableView.numberOfSections >= priorSelectedIndexPath.section+1) && ([tableView numberOfRowsInSection:priorSelectedIndexPath.section] >= priorSelectedIndexPath.row+1)) { 
    NSArray *thePriorIndexPathArray = [NSArray arrayWithObject:priorSelectedIndexPath]; 
    [self.tableView reloadRowsAtIndexPaths:thePriorIndexPathArray withRowAnimation:UITableViewRowAnimationFade]; 
} 


    // Reload only the selected indexPath - necessary to update the text colors etc. 
    NSArray *theIndexPathArray = [NSArray arrayWithObject:indexPath]; 
    [self.tableView reloadRowsAtIndexPaths:theIndexPathArray withRowAnimation:UITableViewRowAnimationFade]; 
} 
관련 문제