2016-08-14 156 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

如果你不会使用自动布局比你必须计算单元格中所有属性的高度,并在返回单元格时给出每个单元格的高度。 –

相关问题