2012-04-18 5 views
2

섹션 및 다중 선택과 함께 tableview를 사용하고 있지만 하나의 행을 선택할 때 여러 행을 확인하는 데 문제가 있습니다 ... 이것에 대해 다른 스레드,하지만 정말 솔루션을하지 않았다 ... 여기 iOS : Tableview 다중 선택 - AccessoryCheckmark 재사용 가능한 셀 확인

내 코드입니다 :

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath 
{  
    [employeeTable deselectRowAtIndexPath:[employeeTable indexPathForSelectedRow] animated:NO]; 

    UITableViewCell *cell = [employeeTable cellForRowAtIndexPath:indexPath];  

    // get the letter in each section 
    NSString *alphabet = [charIndex objectAtIndex:indexPath.section]; 

    // get the names beginning with the letter 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];  

    NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];  

    NSString *name = [names objectAtIndex:indexPath.row]; 

    for(int i = 0; i < [employeeConnection.employees count]; i++) 
    { 
     Employee *aEmployee = [employeeConnection.employees objectAtIndex:i]; 

     NSString *firstName = aEmployee.firstName; 
     NSString *lastName = aEmployee.lastName; 
     NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 

     if([fullName isEqualToString:name]) 
     { 
      NSLog(@"Name: %@", name); 

      if (cell.accessoryType == UITableViewCellAccessoryNone) { 

       cell.accessoryType = UITableViewCellAccessoryCheckmark; 

       // Reflect selection in data model 
       [chosenEmployees addObject:aEmployee.employeeID]; 
       [chosenEmployeesNames addObject:name]; 

      } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 

       cell.accessoryType = UITableViewCellAccessoryNone; 

       // Reflect deselection in data model 
       [chosenEmployees removeObject:aEmployee.employeeID]; 
       [chosenEmployeesNames removeObject:name]; 
      } 
     } 
    } 
} 

업데이트 : 추가 cellForRowAtIndexPath

- (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]; 

     cell.textLabel.textColor = [UIColor whiteColor]; 
    } 

    // Get the letter in the current section 
    NSString *alphabet = [charIndex objectAtIndex:[indexPath section]]; 

    // Get the names beginning with the letter 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; 
    NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate]; 

    if([names count] > 0) 
    { 
     // Extract the name 
     cell.textLabel.text = [names objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 
+0

당신이 그들을 원하지 않을 때 검사되는 재사용 된 세포입니까? 주위를 스크롤 할 때와 마찬가지로? – larsacus

+0

저는 약 14 개의 행을 섹션으로 나누었습니다 ... 예를 들어 첫 번째 행을 선택하면 다른 섹션의 다른 행이 선택됩니다 ... (섹션 - A : 1 (이 행을 확인합니다) 행, C : 1 2, P : 1, S : 1, T : 2) ... – user969043

+0

당신은 할 것입니다. 'cellForRowAtIndexPath :'코드를 게시하거나 셀을 표시 할 셀을 설정하는 모든 방법. – larsacus

답변

10

체크 된 ManagedObject (CoreData를 사용하는 경우) 또는 단순히 체크 된 IndexPath를 NSMutableSet 저장하는 것이 좋습니다. In-CellForRowAtIndexPath : 그러면 셀을 검사해야하는지 확인할 수 있습니다. 세포가 재사용되고 있기 때문에

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *const identifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; 
     cell.textLabel.textColor = UIColor.whiteColor; 
    } 

    if ([self.checkedIndexPaths containsObject:indexPath]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *const cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [table deselectRowAtIndexPath:indexPath animated:NO]; 

    if ([self.checkedIndexPaths containsObject:indexPath]) { 
     [self.checkedIndexPaths removeObject:indexPath]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    else { 
     [self.checkedIndexPaths addObject:indexPath]; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
} 
+0

감사합니다. 매력처럼 작동했습니다! – user969043

+0

기꺼이 도와 드리겠습니다. 그러나'indexPath'를 저장하는 것은 행을 삽입 할 때 문제가 될 수 있다는 것을 기억하십시오.이 경우에는 셀이 나타내는 체크 된 객체를 추적하는 것이 더 유용 할 수 있습니다. –

+0

@ Jenox. 작동하지만 테이블 뷰를 스크롤 할 때 선택한 체크 표시가 사라집니다. –

0

, 당신은 cellForRowAtInexPath 테이블 데이터 소스 방식의 테이블에있는 모든 셀에 대해 ON 또는 OFF로 액세서리 마크를 설정해야합니다.

그래서 cell.accessoryType 셀 속성은 didSelectRow 대리자 메서드가 아니라 cellForRowAtInexPath에서 지정되어야합니다.

didSelectRow에서 배열의 선택된 행을 추적하고 셀 액세서리 표시를 none으로 설정하거나 배열 값에 따라 cellForRowAtInexPath를 선택 표시합니다.

+0

당신의 아이디어는 효과가 있지만,이 경우 NSSet이 NSArray보다 더 유용하다고 생각하지 않습니까? –

+0

NSSet이이 경우 이점을 제공하는 이유는 모르겠지만 그래도 좋을 것입니다. – Lefteris

+0

오브젝트의 순서가 중요하다면'array'를 사용하고, * 없다면'set'을 사용합니다. ;) –