2013-09-04 1 views
0

한 섹션에서 한 번에 하나의 UITableViewCell 만 검사하도록 허용하고 있습니다. 내가 할 일은 체크 된 UITableViewCell (있는 경우)의 선택을 취소하고 현재 선택한 셀을 확인하는 것입니다. 그래서 기본적으로 하나의 CellA가 선택되고 CellB를 선택하면 CellA에서 선택을 취소하고 CellB에서 선택합니다. 여기 하나의 UITableViewCell 섹션에서 체크 허용을 허용

내가 시도하고 달성하기 위해 무슨 짓을했는지의이 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(![self.selectedIndexPaths containsObject:indexPath]) { 
     [self.selectedIndexPaths addObject:indexPath]; 
    } 
    else { 
     [self.selectedIndexPaths removeObject:indexPath]; 
    } 

    if (indexPath.section == 0) { 
     [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
    } 
    else if (indexPath.section == 1) { 
     if (![self.selectedIndexPaths containsObject:indexPath]) { 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
     } 
     else { 
      for (NSIndexPath *indexPath2 in self.selectedIndexPaths) { 
       if (indexPath2.section == 1) { 
        [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES; 
        [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
       } 
      } 
     } 
    } 
    else if (indexPath.section == 2) { 
     if (![self.selectedIndexPaths containsObject:indexPath]) { 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
     } 
     else { 
      for (NSIndexPath *indexPath2 in self.selectedIndexPaths) { 
       if (indexPath2.section == 2) { 
        [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES; 
        [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
       } 
      } 
     } 

    } 

} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     if ([tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) { 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
     } 
     else { 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES; 
     } 
    } 
    else if (indexPath.section == 1) { 
     if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) { 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
     } 
     else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){ 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES; 
     } 
     else { 
      for (NSIndexPath *indexPath2 in self.selectedIndexPaths) { 
       if (indexPath2.section == 1 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) { 
        [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
       } 
      } 
     } 
    } 
    else if (indexPath.section == 2) { 
     if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) { 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
     } 
     else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){ 
      [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES; 
     } 
     else { 
      for (NSIndexPath *indexPath2 in self.selectedIndexPaths) { 
       if (indexPath2.section == 2 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) { 
        [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
       } 
      } 
     } 

    } 
} 

당신이 볼 수 있듯이, 내가 세 개의 섹션이 있고, 첫 번째는 사용자가 원하는 그러나 많은 셀을 선택할 수 있지만, 두 번째 및 세 번째로, 하나에 국한됩니다.

문제점 : 셀을 선택한 다음 다른 셀을 선택할 때마다 작동합니다. 첫 번째 셀을 선택하면 두 셀 모두 확인됩니다.

나는 도움이나 제안을 주시면 감사하겠습니다. 감사!

답변

1

코드가 훨씬 복잡해 보입니다. 나는 섹션 1과 2에 대한 프로퍼티 (NSIndexPath 타입의 프로퍼티)를 만들고, 섹션 0에 대한 배열 프로퍼티를 생성하여 각각의 섹션에서 선택된 셀이나 셀을 추적한다. indexPath를 사용하여 속성 값을 설정하거나 배열의 경우 셀을 탭할 때 indexPath를 추가합니다 (이미 선택되어있는 경우 삭제). 그런 다음 cellForRowAtIndexPath에있는 해당 속성 또는 배열의 상태를 확인하십시오.

@interface TableController() 
@property (strong,nonatomic) NSArray *theData; 
@property (strong,nonatomic) NSIndexPath *selectedPathForSection1; 
@property (strong,nonatomic) NSIndexPath *selectedPathForSection2; 
@property (strong,nonatomic) NSMutableArray *selectedPaths; 
@end 

@implementation TableController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.selectedPaths = [NSMutableArray new]; 
    self.selectedPathForSection1 = nil; 
    self.selectedPathForSection2 = nil; 
} 


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = self.theData[indexPath.section][indexPath.row]; 
    if ([self.selectedPathForSection1 isEqual:indexPath] || [self.selectedPathForSection2 isEqual:indexPath] || [self.selectedPaths containsObject:indexPath]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }else{ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    switch (indexPath.section) { 
     case 0: 
      if (! [self.selectedPaths containsObject:indexPath]) { 
       [self.selectedPaths addObject:indexPath]; 
      }else{ 
       [self.selectedPaths removeObject:indexPath]; 
      } 
      break; 
     case 1: 
      if (![self.selectedPathForSection1 isEqual:indexPath]) { 
       self.selectedPathForSection1 = indexPath; 
      }else{ 
       self.selectedPathForSection1 = nil; 
      } 
      break; 
     case 2: 
      if (![self.selectedPathForSection2 isEqual:indexPath]) { 
       self.selectedPathForSection2 = indexPath; 
      }else{ 
       self.selectedPathForSection2 = nil; 
      } 
      break; 
    } 
    [tableView reloadData]; 
} 
+0

고마워, 나는 그것을 돌려 줄 것이다 ... 내가 다시로드해야합니까? – user2242965

+0

@ user2242965 예, 변경 사항을 보려면 데이터를 다시로드해야합니다 (또는 reloadRowsAtIndexPaths를 사용할 수 있지만 좀 더 많은 코드가 필요하면보다 효율적으로 사용하고 싶을 수도 있습니다). – rdelmar

+0

흠 ... 좋아, 고마워, 지금 이걸 시도해 볼게. – user2242965