2014-05-18 86 views
2

對於我的表格視圖單元格的動態高度,我從此鏈接中引用。 Using Auto Layout in UITableView for dynamic cell layouts & variable row heights帶有動態單元格高度的UITableview中的AutoLayout

Here is custom cell with all constrains

這裏是我的tableview中的數據源和委託方法的代碼

-(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; 

} 

我有2個問題:

  1. 我的問題是我得到的錯誤EXE_BAD_EXCESS附近行

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

    in heightForRowAtIndexPath and estimatedHeightForRowAtIndexPath

  2. 爲什麼我必須在cellForRowAtIndexPathheightForRowAtIndexPath中編寫標籤文本?

此外,我缺少任何需要實現動態高度的單元格?

+0

IBtblAutoLayoutExample是您的tableview的名稱? – EridB

+0

是的,它是IBOutlet我的tableview。 –

+0

我認爲你不需要實現heightForRowAtIndexPath和estimatedHeightForRowAtIndexPath,這可以在你的單元格中直接約束,你只需要設置表視圖的估計行和行高度屬性。 –

回答

2

要爲行高設置自動尺寸&估計的行高,請確保按照以下步驟進行製作,自動尺寸對單元格/行高度佈局有效。

  • 分配和執行的tableview數據源和委託
  • 分配UITableViewAutomaticDimension爲row​​Height & estimatedRowHeight
  • 實施委託/數據源的方法(即heightForRowAt和值UITableViewAutomaticDimension返回到它)

-

目標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; 
} 

夫特:

@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

標籤實例
  • 線= 0的集數(&線中斷模式=截斷尾部)
  • 設置所有約束(頂部,底部,左側)相對於其superview/cell co ntainer。
  • 可選:如果希望標籤覆蓋的最小垂直面積(即使沒有數據),請設置標籤的最小高度。

enter image description here

注意:如果您在多個標籤(UI元素)具有動態長度,應根據其內容的大小進行調整:調整「內容擁抱和抗壓Priority`您想要以更高優先級進行擴展/壓縮的標籤。

相關問題