Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Automatic Dimension Not Working Correctly

I have a tableview that is getting populated with data from Firebase. However, when resizing the tableview using automatic dimension, some text is shown getting cut off.

Here is my Storyboard with constraints set to top, bottom, right, and left.

enter image description here

It is working fine when there is not alot of text as shown here.

enter image description here

However, when I fill the cell with alot of text this occurs.

enter image description here

Here is the code that I am currently using.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (tableView == questInformationTableView) {
            return 50
        }
        else if (tableView == questWalkthroughTableView) {

            return UITableView.automaticDimension
        }

        return 0
    }
    override func viewDidLoad() {
          super.viewDidLoad()
          questWalkthroughTableView.estimatedRowHeight = 250
          questWalkthroughTableView.rowHeight = UITableView.automaticDimension
        }

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

I have also set numberOfLines to 0 and set the label to .sizeToFit()

like image 800
user3667192 Avatar asked Nov 15 '18 17:11

user3667192


People also ask

How does a UITableView work?

A UITableView instance calls these two methods to request the information it needs, piece by piece. The methods of a data source can be called more than once. A UITableView instance does not store any data.

What is the uitableviewdatasource protocol?

While a table view offers many sophisticated features, The UITableViewDataSource protocol has only two required methods. Implementing these two is enough to have a working table view. A UITableView instance calls these two methods to request the information it needs, piece by piece.

Should I use a scroll view or a UITableView?

For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.

How do I use quotecell in UITableView?

QuoteCell The critical line of code in this method is the call to the dequeueReusableCell(withIdentifier:) methods of UITableView. This returns a cell we can reuse, or creates a new one when none is available. The QuoteCell class already handles data formatting, so all we need to do here is pass the quote data to the cell instance.


1 Answers

Don't make the bottom constraint greater than or equal to just set it like the top constraint. Take away the estimatedHeightForRowAt and heightForRowAt functions. Your view did load declarations are sufficient. When you reload data also call self.view.layoutIfNeeded

like image 200
Alexander Avatar answered Oct 04 '22 19:10

Alexander