1

I'm implementing a table view with 3 types of custom cells. As shown below, in the company information section, the headquarters label for my custom table view cell CompanyInfoTableViewCell is getting cut off because I think the custom table cell is not resizing to fit the content view.

enter image description here

I tried implementing this below code in the viewDidLoad method of the controller containing the table view. This code was suppose to make the cells automatically resize to fit the content, but it didn't work (Edit: It didn't work because I'm making the custom cells and table view PROGRAMMATICALLY USING NO STORYBOARD). How do I get my custom table view cell to show all my content?

tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()

CompanyInfoTableViewCell Code:

class CompanyInfoTableViewCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {        
        //Create industry label
        industryLabel = UILabel(frame: CGRect(x: horOffset, y: 0, width: 0.8*screenSize.width, height: 21))
        industryLabel.center.y = 15
        contentView.addSubview(industryLabel)

        
         //Create headquarters label
        hqLabel = UILabel(frame: CGRect(x: horOffset, y: 0, width: 0.8*screenSize.width, height: 21))
        hqLabel.center.y = 40
        contentView.addSubview(hqLabel)
                
    }
}    
14wml
  • 4,048
  • 11
  • 49
  • 97
  • Possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Fay007 Jan 08 '17 at 06:05
  • Please add bottom constraint to your view in storyboard as the estimated row height would not work without it. This is a common issue when your constraints are not working. – Fay007 Jan 08 '17 at 06:05
  • I'm trying to do this programmatically, I don't have a storyboard – 14wml Jan 08 '17 at 06:07
  • you can always add bottom constraint programmatically – Fay007 Jan 08 '17 at 06:09
  • Go with this link, how to give [Constraints programatically](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html) and [UITAbleviewCell constraints programatically](http://stackoverflow.com/questions/18969355/how-to-create-a-custom-uitableviewcell-programmatically-using-autolayout) – dahiya_boy Jan 08 '17 at 06:30
  • @the_dahiya_boy the second link you provided gives an answer in objective c, do you have an answer in swift? – 14wml Jan 08 '17 at 15:01

1 Answers1

1

If you are not using the storyboard and AutoLayout constraints then you need to manually calculate the height which the UILabels are going to take based on the content it holds. I don't see any code which calculates the height based on content and accordingly setting the height of your labels and also the height of your custom cell.

You must implement heightForRowAtIndexPath to set the height for your cells.

Gurdev Singh
  • 1,996
  • 13
  • 11
  • `tableView.estimatedRowHeight = 80 tableView.rowHeight = UITableViewAutomaticDimension` This two methods do the same as `heightForRowAtIndexPath` do. – dahiya_boy Jan 08 '17 at 06:31
  • No, both are same but there is difference in the performance like `.rowHeight`is the property and `heightForRowAtIndexPath` is the delegate method that means if cell is generated then each time delegate method will be called. – dahiya_boy Jan 08 '17 at 06:41
  • You are wrong, check https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html – Gurdev Singh Jan 08 '17 at 07:01
  • Read again this document and check this [Tutorial](https://www.raywenderlich.com/129059/self-sizing-table-view-cells) – dahiya_boy Jan 08 '17 at 07:16