Swift iOS tableView static cell dynamic computing height

Source: Internet
Author: User

Swift iOS tableView static cell dynamic computing height

TableView is a commonly used component in iOS development. In some tables, because UILabel contains different characters in the text, the display height is also different. Therefore, the height of static cell needs to be calculated dynamically. I use static cell. Note that the height of each row must be specified. The height of the cell in the default style is 44, and the dynamic calculation is performed in the third row (row = 2, the fourth line needs to determine whether to display based on whether there is content. If not, the return height is 0.

According to the actual attempt and view of foreign articles

Func tableView (tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) does not seem to work for static tables, or it may be because I have not figured out the correct usage.

Override func tableView (tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)-> CGFloat {if indexPath. row = 2 {return heightForView (task. summary !, Font: UIFont (name: "Helvetica", size: 15.0 )!, Length: 340) + 30} else if indexPath. row = 3 {if task. type = TaskType. TYPE_PLAN.rawValue {if let descr = task. descr {return heightForView (descr, font: UIFont (name: "Helvetica", size: 15.0 )!, Width: 340) + 30 }}else {return 0 }} else if indexPath. row = 4 {return 110} return 44} // calculate the height of func heightForView (text: String, font: UIFont, width: CGFloat)-> CGFloat {let label: UILabel = UILabel (frame: CGRectMake (0, 0, width, CGFloat. max) label. numberOfLines = 0 label. lineBreakMode = NSLineBreakMode. byWordWrapping label. font = font label. text = text label. sizeToFit () return label. frame. height}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.