2009-05-22 2 views

답변

2

당신은 UITableViewCell를 서브 클래 싱하지 않고 그것을 할 수 있습니다.

tableView:cellForRowAtIndexPath: 메서드에서 기본 UITableViewCell을 만들고이 셀에 포함 된 UILabel의 속성을 변경합니다. 셀에 더 많은 텍스트가 들어가도록 최대 줄 수를 변경하십시오.

불행히도 셀 레이블에 속성으로 액세스 할 수 없으므로 subviews 속성으로 가져와야합니다.

물론 구현 세부 사항이 UITableViewCell이므로이 SDK의 향후 릴리스에서 중단 될 수 있습니다. 조심해서 사용하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"YourCellId"]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"YourCellId"] autorelease]; 
     cell.font = [UIFont systemFontOfSize:16]; 
     UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]; 
     [cellLabel setNumberOfLines:3]; 
    } 
    cell.text = @"Your text"; 
    return cell; 
} 

업데이트 : 셀

1 세트 :

텍스트에 맞게 행 높이를 설정하려면 다음을 수행 할 수 있습니다 여기에

은 예입니다 텍스트에 맞게 높이.

CGSize textSize = [text sizeWithFont:font 
        constrainedToSize:CGSizeMake(313, 1000) 
         lineBreakMode:UILineBreakModeWordWrap]; 
cell.frame = CGRectMake(0.0f, 0.0f, 320.0, textSize.height); 

2. 돌아 가기 tableView:heightForRowAtIndexPath: 방법 셀 높이 :

당신이 뭔가를해야합니다.

이 같은 예를 들어

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell* cell = [self tableView:(UITableView*)self.view cellForRowAtIndexPath:indexPath]; 
    CGRect cellFrame = cell.frame; 
    return cellFrame.size.height; 
} 
+0

감사합니다! 하지만 내 awakeFromNib에서 나는이 코드를 가지고있다 "self.jokeTableView.rowHeight = 70.0;" 얼마나 많은 텍스트가 들어가야하는지에 따라 rowHeight가 변경되도록하려면 어떻게해야합니까? –

+0

답안에서이 작업을 수행하는 방법에 대한 예제를 추가했습니다. 희망이 도움이됩니다. – henning77

0

서브 클래스 UITableViewCell, 서브 클래스의 loadView 방법은, 그 contentView 내부 UILabel의를 만듭니다. 적절한 포장과 위치를 갖도록이 라벨을 설정하십시오.

+0

나는이 방법을 지정할 수 있습니까? - (있는 UITableViewCell *) tableViewCell : (jQuery과 *)있는 tableView cellForRowAtIndexPath : (NSIndexPath *) indexPath –

+0

내가 텍스트 크기를 변경, 그리고 당신은 그 방법 모두를 지정할 수 없습니다 –

+0

근무 곳이기 때문에. 표시 할 텍스트의 길이에 따라 각 셀의 높이를 변경해야합니다. 또한 UILabel이 표시 할 수있는 최대 줄 수를 1보다 큰 숫자로 설정해야합니다. – mmc

0

아담이 맞습니다. 조금 더 자세하게 (번개를 빠르게 스크롤하는 방법뿐만 아니라) atebits.com에서 UILabel 방식을 완전히 무시하는이 항목을 확인할 수 있습니다.

Loren Brichter's UITableViewCell subclassing example

또한 developer.apple.com에서 TableViewSuite의 예를 볼 수 있습니다 그것은 사용자 정의 TableViews을 구축, 복잡성 증가로, 5 개 가지 예제가 포함되어 있습니다. 마지막 예제는 atebits.com의 튜토리얼과 구조적으로 매우 비슷합니다.

스탠포드 iPhone 강의의 강의 8은 Scroll 및 TableViews에 관한 것이기도하며 UITableViewCell을 서브 클래 싱하는 몇 가지 예가 있습니다.

iPhone Course

관련 문제