There are 3 main things you need for dynamic height:
(Xcode 7.3, Swift 2.2)
- Set estimatedRowHeight
- Set rowHeight to UITableViewAutomaticDimension
- Set the label's numberOfLines property in your cell to zero
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var tableData: [String] = ["Rod", "Mark", "Lem is from the Philippines. He is a Filipino!", "Evans"] override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.estimatedRowHeight = 44; tableView.rowHeight = UITableViewAutomaticDimension; } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableData.count } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("cell") if cell == nil { cell = UITableViewCell(style: .Default, reuseIdentifier: "cell"); } cell!.textLabel?.text = tableData[indexPath.row] cell!.textLabel?.numberOfLines = 0 return cell! } }
(Xcode 7.3, Swift 2.2)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.