2011-05-10 9 views
1

우리가 배열의 10 개 항목이있는 경우, 다음 각 indexPath.rowdequeueReusableCellWithIdentifier : 다른 indexPath를 어떻게 처리합니까?

내 질문 10 개 세포를 ALLOC합니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    static int count = 0; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     NSLog(@"count %d", count++); 
    } 
    // Configure the cell... 
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    return cell; 
} 

우리의 tableview의 일부 표준 코드를 살펴 보자하는 기능 reuseIdentifier 어떻게하는지 입니다 : CellIdentifier 다른 행을 알고 계십니까?

답변

3

우리가 배열의 10 개 항목, 다음이 사실이 아니다 각 indexPath.row

10 개 세포를 ALLOC 할 경우 - 할당 세포의 일반적으로 수있을 것를 NumberOfVisibleRows + 2 (또는 비슷한 가치).

이것은 (대략) UITableView의 작동 방식입니다. 특정 셀이 가시 영역 밖으로 스크롤되면 UITableView 뷰 계층 구조에서 제거됩니다 (테이블의 clipToBounds 속성을 NO로 설정하면 볼 수 있습니다!). 어떤 종류의 내부 캐시. dequeueReusableCellWithIdentifier: 메서드는 해당 캐시에서 재사용 할 수있는 셀이 있는지 확인한 다음이를 반환하고 (사용할 수있는 셀이없는 경우 nil을 반환합니다.) 따라서 실제로 더 많은 셀을 할당 할 필요가 없습니다.

dequeueReusableCellWithIdentifier 메서드는 현재 indexPath가 아니라 전달 된 셀 문자열 식별자에 의존합니다. 그리고 indexPath는 셀에 적절한 내용을 가져 오기 위해서만 사용됩니다.

-1

오, 방법 **dequeueReusableCellWithIdentifier**:은 indexPath와 아무 관련이 없습니다.

UITableView를 사용할 때 [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault] reuseIdentifier:cellIdentifier]을 사용하여 'cellIdentifier'키가있는 캐시 (매우 NSMutableDictionary 일 수 있음)에 아주 종류의 셀을 넣습니다. 그런 다음 하나 이상의 셀이 필요할 때 캐시를 먼저 쿼리합니다. 누락되면 새 캐시를 만듭니다.

indexPath은 코더가 처리해야 할 책임이 있습니다. 섹션별로 여러 종류의 셀을 구분하고 한 섹션에서 셀을 한 행씩 표시합니다. 섹션 및 행은 NSIndexPath입니다.

관련 문제