2014-05-18 6 views
2

테이블 뷰 셀의 동적 높이의 경우이 링크에서 참조를 가져옵니다.동적 셀 높이가있는 UITableview의 AutoLayout

  1. 내 문제는 내가 오류를 얻을 수 있습니다 : 여기 Here is custom cell with all constrains

    Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
        { 
         return arrTemp. count; 
        } 
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *[email protected]"AutoLAyoutCell"; 
    
    AutoLayoutTableViewCell *cell=(AutoLayoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell==nil) { 
        for (id currentObject in [[NSBundle mainBundle] loadNibNamed:@"AutoLayoutTableViewCell" owner:self options:nil]) { 
         if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
          cell = (AutoLayoutTableViewCell *)currentObject; 
          break; 
         } 
        } 
    } 
    
    cell.IBlblLineNo.text=[NSString stringWithFormat:@"Line:%i",indexPath.row]; 
    cell.IBlblLineText.text=[arrTemp objectAtIndex:indexPath.row]; 
    [cell setNeedsUpdateConstraints]; 
    [cell updateConstraintsIfNeeded]; 
    
    CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByTruncatingTail]; 
    cell.IBlblLineText.numberOfLines=expectedlineLabelSize.height/17; 
    
    CGRect frmlbl=cell.IBlblLineText.frame; 
    frmlbl.size.height=expectedlineLabelSize.height; 
    cell.IBlblLineText.frame=frmlbl; 
    
    return cell; 
    } 
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    
    AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath]; 
    
    cell.IBlblLineNo.text=[NSString stringWithFormat:@"Line:%i",indexPath.row]; 
    cell.IBlblLineText.text=[arrTemp objectAtIndex:indexPath.row]; 
    
    [cell setNeedsUpdateConstraints]; 
    [cell updateConstraintsIfNeeded]; 
    
    CGSize expectedlineLabelSize = [cell.lineLabel.text sizeWithFont:cell.lineLabel.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByWordWrapping]; 
    
    cell.IBlblLineText.numberOfLines=expectedlineLabelSize.height/17; 
    CGRect frmlbl=cell.IBlblLineText.frame; 
    frmlbl.size.height=expectedlineLabelSize.height; 
    cell.IBlblLineText.frame=frmlbl; 
    
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 
    height += 1.0f; 
    
    return height; 
    } 
    
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath]; 
    
    CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByTruncatingTail]; 
    
    return expectedlineLabelSize.height; 
    
    } 
    

    나는이 개 질문이있는 tableview 데이터 소스 및 위임 방법의 내 코드입니다 EXE_BAD_EXCESS 라인 근처

    AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath]; 
    

    heightForRowAtIndexPathestimatedHeightForRowAtIndexPath입니다.

  2. cellForRowAtIndexPathheightForRowAtIndexPath에 모두 라벨 텍스트를 써야하는 이유는 무엇입니까?

또한 동적 높이를 얻는 데 필요한 것이 누락 되었습니까?

+0

IBtblAutoLayoutExample은 테이블 뷰의 이름입니까? – EridB

+0

예, 내 tableview에 IBOutlet입니다. –

+0

heightForRowAtIndexPath 및 estimatedHeightForRowAtIndexPath를 구현할 필요가 없다고 생각합니다. 이는 셀의 직접 제약 조건으로 가능할 수 있으며 테이블 뷰의 행 및 행 높이 추정치 속성을 설정하기 만하면됩니다. –

답변

2

행 높이의 자동 치수를 & 행 높이로 설정하려면 셀/행 높이 레이아웃에 자동 치수를 적용하려면 다음 단계를 수행하십시오.

  • 지정하고있는 tableview 데이터 소스를 구현하고 rowHeight를 &에
  • 지정 UITableViewAutomaticDimension을 위임 estimatedRowHeight는
  • (즉 heightForRowAt과에 값 UITableViewAutomaticDimension을 반환) 대표 /은 dataSource 방법을 구현

-

목표 C :

// in ViewController.h 
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

    @property IBOutlet UITableView * table; 

@end 

// in ViewController.m 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.table.dataSource = self; 
    self.table.delegate = self; 

    self.table.rowHeight = UITableViewAutomaticDimension; 
    self.table.estimatedRowHeight = UITableViewAutomaticDimension; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return UITableViewAutomaticDimension; 
} 

스위프트 : 라인 = 0의

@IBOutlet weak var table: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Don't forget to set dataSource and delegate for table 
    table.dataSource = self 
    table.delegate = self 

    // Set automatic dimensions for row height 
    table.rowHeight = UITableViewAutomaticDimension 
    table.estimatedRowHeight = UITableViewAutomaticDimension 
} 



// UITableViewAutomaticDimension calculates height of label contents/text 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

있는 UITableViewCell

의 레이블 예를 들어
  • 설정 번호 (& 줄 바꿈 모드 = 절단 꼬리)
  • 모든 설정 수퍼 뷰/셀 공동과 관련하여 제약 조건 (위, 아래, 오른쪽 왼쪽) 창녀.
  • 선택 사항 : 데이터가없는 경우에도 레이블로 덮을 최소 수직 영역을 원하는 경우 레이블의 최소 높이를 설정하십시오.

enter image description here

: 당신은 컨텐츠 크기에 따라 조정해야 동적 길이, 더 이상의 라벨 (UiElements의)를 한 경우 '에 대한 내용 포옹 및 압축 저항 Priority`를 조정 우선 순위를 높이려면 확장/압축하려는 레이블을 선택하십시오.

관련 문제