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}