0

I created UITableViewController to show reviews, shown in below picture.

It shows the layout correctly for the first build like below.

Correct Layout

But when I close the app and re-open it, It shows the layout like below.

Wrong Layout

I am using Xcode 12.0 and I tried on iOS 12,iOS 13 and iOS 14 same problem occurs.

Here is my code for the app: (It's a Table View Controller)

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func addTapped(_ sender: Any) {
    self.present(AlertService().ReviewAlert(), animated: true, completion: nil)
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableView.layoutIfNeeded()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 5
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewCell", for: indexPath) as! reviewCell
    
    // Configure the cell...
    cell.ratingSetup(rating: (indexPath.row + 1))
    
    cell.reviewLabel.text = String(repeating: "Lorem Ipsum ", count: ((indexPath.row + 1) * 5))
    
    if indexPath.row == 1 || indexPath.row == 3 { cell.markedAsRead.isHidden = true }
    
    cell.sizeToFit()
    cell.reviewLabel.numberOfLines = 0
    cell.layoutIfNeeded()
    
    return cell
    
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return CGFloat(100)
} }

Here is my Table View Cell Content as well.

  • Please add `tableView.estimatedRowHeight = 40` in the viewDidLoad method. and 1. tableView's height is `UITableView.automaticDemension` by default. so you don't have to override `heightForRowAt` method. 2. you don't have to add `tableView.layoutIfNeeded` in `viewWillLayoutSubviews`. 3. cell.sizeToFit() & cell.layoutIfNeeded() are also not needed. maybe you set constraints wrongly in the storyboard – Li Jin Sep 23 '20 at 12:14
  • Thanks but I already tried and nothing changed. What is necessary in constraints? I also added my Cell's Content and Constraints at the bottom of the post if you would like to check – Semih Özçelik Sep 23 '20 at 13:24
  • What happens if you get rid of your `viewWillLayoutSubviews()` func? It really shouldn't be needed, and *possibly* is causing the issue. – DonMag Sep 23 '20 at 15:29
  • I did delete that part but still having problem. – Semih Özçelik Sep 23 '20 at 16:25

2 Answers2

0

Remove methods:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableView.layoutIfNeeded()
}

...
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return CGFloat(100)
} }

Remove:

cell.sizeToFit()
    cell.reviewLabel.numberOfLines = 0
    cell.layoutIfNeeded()

Set numberOfLines right in the storyboard for the label.

Check cell size field in the storyboard to be empty.

Check if you have all constraints - try to make the cell longer in the storyboard and check how label is rendered there (add long text to check if new lines are added).

Check if when launched you have no constraint warnings, if you have, then find "label to bottom" constraint and set priority from 1000 to 999.

Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
  • Thanks for the answers I did all of your suggestions but still it doesnt work when I re-open the app. It only works on first lounch. – Semih Özçelik Sep 23 '20 at 16:24
0

I don't know why it happened but found a solution.

I added all other view to a Header.xib file and used basic cell type for review text.

Then It worked. Probably Cell can't handle auto dimension when there is bunch of stuff going on the prototype cell.