2009-08-12 2 views
0

많은 iPhone 응용 프로그램에서 체크 상자 목록으로 사용되는 UITableViewController가 나타납니다. (내 말은, 설정의 자동 잠금 기능을 참조하십시오.)체크 상자 목록으로 UITableViewController를 사용할 때 기본 항목 선택

이 기능을 직접 구현하는 동안 기본적으로 프로그래밍 방식으로 항목을 선택하려면 여러 가지 기능을 사용해야했습니다. , 목록이 나타내는 현재 값).

- (void)viewDidAppear:(BOOL)animated { 
    NSInteger row = 0; 

    // loop through my list of items to determine the row matching the current setting 
    for (NSString *item in statusItems) { 
     if ([item isEqualToString:currentStatus]) { 
      break; 
     } 
     ++row; 
    } 

    // fetch the array of visible cells, get cell matching my row and set the 
    // accessory type 
    NSArray *arr = [self.tableView visibleCells]; 
    NSIndexPath *ip = [self.tableView indexPathForCell:[arr objectAtIndex:row]]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:ip]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    self.lastIndexPath = ip; 

    [super viewDidAppear:animated]; 
} 

이 특정 셀과 indexPath 경우에 대한 참조를 가져이 가장 좋은/만/가장 쉬운 방법입니다 : 가장 좋은 나는 내보기 컨트롤러 클래스의 viewDidAppear 방법을 재정 의하여이다 마련 할 수있었습니다 기본적으로 행을 표시하고 싶습니다.

답변

1

상태 항목을 표시하려면 tableView:cellForRowAtIndexPath:을 구현해야합니다. 그렇습니까? 그럼, 왜 그냥이 같은 셀을 반환하기 전에 셀의 액세서리 유형을 설정하지 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // dequeue or create cell as usual 

    // get the status item (assuming you have a statusItems array, which appears in your sample code) 
    NSString* statusItem = [statusItems objectAtIndex:indexPath.row]; 

    cell.text = statusItem; 

    // set the appropriate accessory type 
    if([statusItem isEqualToString:currentStatus]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

코드는 충격에, 당신은 [self.tableView visibleCells]를 사용 특히 때문이다. 화면에 맞는 행보다 상태 항목이 더 많은 경우 (이름에서 알 수 있듯이 visibleCells은 현재 보이는 표보기의 셀만 반환합니다)?

+0

나는 그 코드에 멍청하다고 생각했다. 오늘은 천천히 두뇌 : P – Dana

관련 문제