2010-12-09 6 views
1

유용한 지침에 따라 xib 파일에서 사용자 정의 UITableViewCell을로드했습니다. How do you load custom UITableViewCells from xib files.cellForRowAtIndexPath의 다른 UITableViewCell 유형을 반환하십시오.

UITableView의 첫 번째 행에 대해서만이 사용자 정의 셀을 원한다. 나머지는 간단한 레이블이고 표준은 UITableViewCell이다.

문제는 사용자 정의 셀을 포함하면 전체 UITableView이 표시되지 않고 탐색 요소를 제외하고 화면이 비어있는 것입니다. 표준 셀을 사용하면 표가 잘 나타나므로 사용자 정의 셀과 관련이 있습니다.

여기에 셀을 반환해야하는 코드가 있습니다.

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

    if(indexPath.row == 0) { 
     ArticleDetailCell *cell = (ArticleDetailCell *)[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@Detail", reuseIdentifier]]; 

     if(!cell) { 
      // Load the top-level objects from the custom cell XIB. 
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ArticleDetailCell" owner:self options:nil]; 
      // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
      cell = (ArticleDetailCell *)[topLevelObjects objectAtIndex:0]; 
     } 

     [[(ArticleDetailCell *)cell judges] setText:[[self caseBaseArticle] judges]]; 
     [[(ArticleDetailCell *)cell judgementDate] setText:[[self caseBaseArticle] judgmentDate]]; 
     [[(ArticleDetailCell *)cell court] setText:[[self caseBaseArticle] court]]; 

     return cell; 

    } else {  
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

     if(!cell) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]; 
      [[cell textLabel] setNumberOfLines:1]; 
      [[cell textLabel] setFont:[UIFont systemFontOfSize:14]]; 
      [[cell textLabel] setText:articleRowTitles[indexPath.row-1]]; 
     } 

     if ([self tableRowHasContentDetail:indexPath]) { 
      [self enableTableCell:&cell]; 
     }else { 
      [self disableTableCell:&cell]; 
     }  

     return cell; 
    } 
} 

ArticleDetailCell은 XIB 파일에서 확인로드와 속성 내가 디버거에서 볼 수 올바르게 설정하세요. 내가 (gdb)에서 사용자 정의 셀 객체를 인쇄 할 때 return cell에서 멈춤.

<ArticleDetailCell: 0x4bc22b0; baseClass = UITableViewCell; frame = (0 0; 320 367); autoresize = W+TM+BM; layer = <CALayer: 0x4bc23c0>> 

세포 역 참조에는 어떤 문제가 있습니까? 나는 여전히 카운트를 유지할 때와 카운트가 증가하거나 증가하지 않을 때 내 머리를 터뜨릴 것입니다.

단일 셀에 문제가있어서 전체 UITableView가 사라지는 이유는 무엇입니까?

편집 :은 참고 @BoltClock

- (void)disableTableCell:(UITableViewCell **)cell { 
    [*cell setAccessoryType:UITableViewCellAccessoryNone]; 
    [*cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    [[*cell textLabel] setTextColor:[UIColor lightGrayColor]]; 
} 
+0

그냥 급히 : 당신의 당신이 당신의 라벨을 사용자 정의 할 때, 당신은 당신이 이미 선언하고'ArticleDetailCell'로 초기화하기 때문에 ArticleDetailCell' 때마다'에'cell' 캐스팅하지 않아도 문합니다. 'enableTableCell :'과'disableTableCell :'메소드는 어떻게 생겼을까요? – BoltClock

+0

@BoltClock은 리팩터링 힌트를 주셔서 감사합니다. 이전에 UITableViewCell로만 사용자 정의 셀을 생성하는 문제를 해결했습니다. –

+0

@BoltClock은 disableTableCell 메소드를 질문에 추가했습니다 (활성화는 거의 같지만 다른 값을 사용함). –

답변

1

문제는 대부분 사용자 지정 셀의 xib 파일에서 발생했습니다. Apple 개발자 센터에서 Loading Custom Cells from NIB Files을 엄격하게 따르는 것이 트릭을 만들었습니다.

중요한 점은 사용자 정의 셀의 File's Owner을 셀을 사용할 UITableViewController 유형으로 설정하는 것이 었습니다. UITableViewController에는 을 xib 파일의 사용자 지정 셀에 연결하는 데 사용 된 사용자 지정 UITableViewCell에 대한 IBOutlet 속성이 있습니다.

중요한 점은 Identifier가 dequeueReusableCellWithIdentifier 메서드에서 사용 된 값과 일치하도록 표보기 셀의 xib 파일에 설정된다는 것입니다.

cellForRowAtIndexPath은 다음과 같이 끝납니다. articleDetailIBOutlet 속성입니다.

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

    if(indexPath.row == 0) { 
     ArticleDetailCell *cell = (ArticleDetailCell *)[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@Detail",reuseIdentifier]]; 

     if(!cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"ArticleDetailCell" owner:self options:nil]; 
      cell = articleDetail; 
      [self setArticleDetail:nil]; 
     } 

     [[cell judges] setText:[[self caseBaseArticle] judges]]; 
     [[cell judgementDate] setText:[[self caseBaseArticle] judgmentDate]]; 
     [[cell court] setText:[[self caseBaseArticle] court]]; 

     return cell; 

    } else {  
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

     if(!cell) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]; 
      [[cell textLabel] setNumberOfLines:1]; 
      [[cell textLabel] setFont:[UIFont systemFontOfSize:14]]; 
      [[cell textLabel] setText:articleRowTitles[indexPath.row-1]]; 
     } 

     if ([self tableRowHasContentDetail:indexPath]) { 
      [self enableTableCell:&cell]; 
     }else { 
      [self disableTableCell:&cell]; 
     }  

     return cell; 
    } 
} 
1

어디 정의 reuseIdentifier입니까? 항상이 방법에 사용할 수 있습니까?

+0

예, UIViewController 구현 내 정적 NSString입니다. –

관련 문제