2016-08-14 154 views
-1

我有一個動態單元格高度的問題。這是我的代碼,單元格不是動態高度,hơ使它動態(不是自動佈局)。感謝swift中的動態單元格高度

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomCell 


     let label = UILabel(frame: CGRectMake(0 , 0, cell.frame.width, cell.frame.height)) 
     label.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eget libero enim. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec tempor posuere augue. Pellentesque at nibh quam. Mauris at nulla id enim elementum fermentum sit amet at dolor. Phasellus quis purus eu enim ullamcorper porttitor. Praesent consectetur lorem non ligula elementum rhoncus. Pellentesque diam dolor, gravida eget quam at, consequat luctus elit. Curabitur posuere augue sed nunc ornare semper. Nunc ut lorem vitae ligula dictum dapibus quis eget ex. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas." 
     label.numberOfLines = 0 
     label.sizeToFit() 
     label.lineBreakMode = NSLineBreakMode.ByWordWrapping 
     cell.contentView.addSubview(label) 

     return cell 
    } 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 
    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

回答

0

在SWIFT 3.0

你已經確立了自己的標籤約束像龍頭,traling,頂部和底部

然後設置後,約束你ViewController.swift文件的

首先

方法ViewDidLoad()寫這個代碼。

yourTableview.estimatedRowHeight = 83.0

yourTableview.rowHeight = UITableViewAutomaticDimension

+0

@shuuichiakai如果它是有用的,然後使upvote謝謝 – Jaydip

1

您可以使用NSStringboundingRectWithSize方法或NSAttributedString

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    let text: NSString = "" 
    let size = CGSize(width: widthOfYourCell, height: CGFloat.max) 
    let attributes: [String : AnyObject] = [ 
     NSFontAttributeName : UIFont.systemFontOfSize(15) 
     // etc. 
    ] 
    let rect = text.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: attributes, context: nil) 
    return rect.height 
} 
+0

謝謝,你是天才:)) – shuuichiakai

+0

@shuuichiakai歡迎您。接受答案,如果它可以幫助你) – Jauzee

1

您必須添加以下代碼:

override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.rowHeight = UITableViewAutomaticDimension; 
     self.tableView.estimatedRowHeight = 44.f; // As your mind. 
    } 
+0

如果我使用自動佈局,沒關係,但即時消息不使用自動佈局:) – shuuichiakai

0

只需添加的tableview這個問題的兩個委託方法,看到了魔術。請記住不要給任何財產的固定高度。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
return UITableViewAutomaticDimension 
} 

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
return 500 
} 
+0

如果我使用自動佈局,沒關係,但我沒有使用自動佈局 – shuuichiakai

+0

如果你不會使用自動佈局比你必須計算單元格中所有屬性的高度,並在返回單元格時給出每個單元格的高度。 –

相關問題