Setting Custom Uitableviewcells Height

custom UITableviewcell height not set correctly

You can use self-sizing cells in iOS 8 (Swift code):

tableView.estimatedRowHeight = 88.0
tableView.rowHeight = UITableViewAutomaticDimension

Two tutorials list below:

  1. Understanding Self Sizing Cells and Dynamic Type in iOS 8
  2. iOS 8: Self Sizing Table View Cells with Dynamic Type

getting the height of the custom cell in UITableViewCell

You can use Autolayout for the same and get free from all the coding part. Just set the constraint of the inner view in association with the cell and then right the following code.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell:ChatSenderTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "ChatSenderTableViewCell", for: indexPath) as? ChatSenderTableViewCell)!

cell.LabelChatMessage.text = "Your Message"
cell.LabelChatMessage.numberOfLines = 0
cell.LabelChatMessage.sizeToFit()

return cell

}

I am using a custom cell name ChatSenderTableViewCell to create a bubble like effect containing a label for messages send or receive.

Custom UITableViewCell Height

You can change the height of all of your cells in the definition of the tableView, but to set them individualy you have to use the delegate method. It's confusing that you can set it in IB but it is for display purposes only and not used at runtime.



Related Topics



Leave a reply



Submit