2016-10-26 2 views
0

언제든지 확인할 수있는 항목을 최대 3 개까지만 저장합니다.UITableViewCell 액세서리 체크 표시가 행 선택 후 스크롤되지 않음

나는에 그런 didSelectRowAtIndexPath

selectedRowDictionary를라는 NSMutabeDictionary에서 선택한 행을 저장 내 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; 

if (_selectedRowDictionary && [_selectedRowDictionary count]) { 
    for (NSString *rowSelected in _selectedRowDictionary) { 
     BOOL isRowSelected = [[_selectedRowDictionary valueForKey:rowSelected] integerValue]; 
     if (isRowSelected){ 
      NSLog(@"rowSelected: %@", rowSelected); 
     } else { 
      NSLog(@"rowNotSelected: %@", rowSelected); 
     } 

     int rowIndexSelected = [[rowSelected substringFromIndex:[rowSelected length] - 1 ] integerValue]; 

     if (isRowSelected && rowIndexSelected == indexPath.row) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } else { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } 
}  
return cell; 
} 

- - didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 


if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if (_selectedRowDictionary) { 
     [_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; 
     NSLog(@"row %d removed from array", indexPath.row); 
    } 

} else { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [_selectedRowDictionary setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; 
} 

if ([_selectedRowDictionary count] > 3) { 
    UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    lastSelectedCell.accessoryType = UITableViewCellAccessoryNone; 

    [_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; 
    NSLog(@"row selected > 3, row%d not selected", indexPath.row); 
} 
} 

가 나는 원인이 뭔가를 놓치고 있습니까 내가 아래로 스크롤 할 때 셀이 다시 선택 취소됩니다. 그는 tableview?

나는 사전을 NSLog

, 그것은 내가 이미 전지 재활용 문제를 해결할 생각, 그 행이 존재하고 내가 비슷한 질문을 확인하신

을 선택되었습니다 있다고하지만.

답변

1

대신 가변 배열을 자유롭게 사용할 수 있습니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; 

    if ([_selectedRowArray containsObject:indexPath]) 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    else 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    return cell; 
} 


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

    if ([_selectedRowArray containsObject:indexPath]) { 
     [_selectedRowArray removeObject:indexPath]; 
    } else { 
     if (_selectedRowArray.count < 3) 
      [_selectedRowArray addObject:indexPath]; 
     else { 
      // Don't select it 
     } 
    } 

    [tableView reloadData]; 
} 
+0

'NSMutableArray'에 대한 표시 인터페이스가 없습니다. 선택기 removeObjectIndexPath'가 선언 되었습니까? 'removeObjectAtIndex : indexPath.row'를 원하셨습니까? – Simon

+0

콜론을 놓쳤습니다. 내 대답을 업데이트했습니다. – norders

+0

감사! 이것은 이전의 시도에서 무엇이 잘못 되었는가를 알고 있습니까? – Simon

1

코드를 편집하고 있습니다.이 코드를 사용해보십시오. 에서

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; 
    BOOL isRowSelected = [[_selectedRowDictionary valueForKey:@(indexPath.row)] boolValue] 
    if (isRowSelected) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    return cell; 
} 

코드 변경 - didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 


if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if (_selectedRowDictionary) { 
     [_selectedRowDictionary removeObjectForKey:@(indexPath.row)]; 
     NSLog(@"row %d removed from array", indexPath.row); 
    } 

} else { 
    if (_selectedRowDictionary.count == 3) { 
     // Don't allow for Selection 
     return; 
    } 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [_selectedRowDictionary setValue:@(YES) forKey:@(indexPath.row)]; 
    } 
} 

사용자가 새 행을 선택하기 이전에 선택한 행을 취소 할 수 있습니다. 최적화 된 방식으로 코드를 편집 했으므로이 코드가 도움이되기를 바랍니다.

관련 문제