nm1213
nm1213

Reputation: 99

UITableView.automaticDimension not working

I've got a problem with a prototype cell (which is populated from Firebase), where the cell height is not being set correctly despite constraints being set.

I've tried setting the following:

override func viewDidLoad() {
    super.viewDidLoad()

    TableViewPVV.rowHeight = UITableView.automaticDimension
    TableViewPVV.estimatedRowHeight = 100

}

This does not work. When using UITableView.automaticDimension, the cell height defaults to 44.

However, if the cell height can be expanded if I set TableViewPVV.rowHeight explicitly to a value like 100.

NOTE: I also need to explicitly define the label's desired width or the rowHeight value specified is ignored.

ALSO NOTE: I have the following constraints set on the label: Trailing Space to Superview = 2, leading space = 2, Bottom space >= 2, Top Space >= 2.

Some screenshots as follows:

Nayem: edited to add a new image for you of what the content looks like without setting a desired width, and with the height set to 100.

import UIKit
import Firebase


class PVVViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var PVVList = [PVVModel]()

    @IBOutlet weak var TableViewPVV: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()



        LoadDataFromPVV()

         // TableViewPVV.rowHeight = 100
        TableViewPVV.rowHeight = UITableView.automaticDimension
        TableViewPVV.estimatedRowHeight = 100

    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        return PVVList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        //creating a cell using the custom class

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


        //the PVV object
        let PVV: PVVModel

        //getting PVV for selected position
        PVV = PVVList[indexPath.row]

        //adding values to labels
        cell.PVVData.text = PVV.BodyText

        //returning cell
        return cell  
    }


     func LoadDataFromPVV() {


        databaseReference = Database.database().reference()


        let refPVV = Database.database().reference(withPath: "PVV").queryOrdered(byChild: "Status").queryEqual(toValue: "Active")

        refPVV.observeSingleEvent(of: .value, with: { (snapshot) in

            //if the reference have some values
            if snapshot.childrenCount > 0 {

                //clearing the list
                self.PVVList.removeAll()


                //iterating through all the values
                for PVV in snapshot.children.allObjects as! [DataSnapshot] {
                    //getting values
                    let PVVObject = PVV.value as? [String: AnyObject]
                    let PVVText  = PVVObject?["BodyText"]


                    //creating PVV object with model and fetched values
                    let PVV = PVVModel(BodyText: PVVText as! String?)

                    //appending it to list
                    self.PVVList.append(PVV)
                 }

                //reloading the tableview
                self.TableViewPVV.reloadData()
            }

        })
    }
}

I'm hoping to be able to dynamically adjust the height of my label (PVVData) to fit the text as captured from Firebase.

Upvotes: 2

Views: 503

Answers (3)

Shezad
Shezad

Reputation: 756

Just try the below method :

set constraints as shown in the image below (without minimum height).

enter image description here

set constraints as shown in the image below (with a minimum height).

for a minimum height you requiure a height constraint.

enter image description here

enter image description here

Set number of lines to 0.

enter image description here

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

Upvotes: 1

Parth Patel
Parth Patel

Reputation: 919

Please add this method in your class

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

Make sure label attribute like this:

enter image description here

And Set proper constraint for label.

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13843

In most cases it's the issue of constraints .... Please make sure that your top label is top aligned with contenview and your bottom most label is aligned with bottom constraint of the contentView.

see this example

enter image description here

Upvotes: 1

Related Questions