2

The problem I have is that I am using self-sizing cells, and estimated row height.

My tableview is a non scrollable table in a scrollview. I have a variable for the table height which I initially set to 0.

I am trying to update my tables height to be the right size depending on how many cells will fill it and depending on the size those cells will take up. I am trying to use tableview.contentSize.height to get the tables height and set the variable to that size.

I then recall my alignment function to reset the tables height. When I build and run the tables size isn't big enough to show all of the cells that it should.

How can I fix this?

2
  • If you absolutely need to have a non scrollable table then you will have to loop through your table data, call cellForRowAtIndexPath to get all the cells, then sum the heights. But really you should not have a non-scrollable table (I'm assuming that you are putting it inside a scrollview).
    – Putz1103
    Jul 6, 2016 at 17:48
  • yes i am using it in a scroll view. To sum all the heights, how would i do that? Jul 6, 2016 at 19:17

1 Answer 1

2

You can do it from the following steps ->

  1. Construct the tableview within the UIView with 0 Leading, Top, bottom, Trailing constant
  2. Add height constraint of the UIView and define outlet of it

enter image description here 3. Load Data in the tableview and get tableview's ContentHeight

  1. Update the height constraint of the UIView equal to the tableview's ContentHeight

Dont forget to call layoutIfNeeded() after updating constraint

class ViewController: UIViewController {

    @IBOutlet weak var containerViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var nameTableView: UITableView!

    var dataArrary = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
        nameTableView.rowHeight = UITableViewAutomaticDimension
        nameTableView.estimatedRowHeight = 44.0
        nameTableView.tableFooterView = UIView(frame:CGRectZero)
        nameTableView.tableHeaderView = UIView(frame:CGRectZero)
        dataArrary = ["item1 bshfklsdflkjsdfkljsdklfjlsdkfjlksdjflksdjflkdsjflkdsjflksdjflkjdslkfjdslkjflkdsfjdsfkljsdflkjdslkfj", "item2hfdshjgfhjdsgfhjdgfhjdgfhjdgfhjgfhsdf", "item3", "item4", "item5", "item6kfjdskfljsdlkfjlksdfjlksdjfkldsjfklfjdkslf"]

    }

    override func viewDidAppear(animated: Bool) {

        updateView()
    }

    func updateView() {

        self.nameTableView.scrollEnabled = false

        self.nameTableView.frame.size = CGSizeMake(self.view.frame.width, self.nameTableView.contentSize.height)
        containerViewHeightConstraint.constant = self.nameTableView.frame.height 
       // assign height equal to the tableView's Frame
        self.view.layoutIfNeeded()  
       // redraws views and subviews with updated constraint

    }
}

extension ViewController:UITableViewDataSource {

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

        return dataArrary.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
         cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = dataArrary[indexPath.row]      
        return cell  
    } 

}
  1. It should look like this

enter image description here

9
  • what is layoutIfNeeded() and how do i use it Jul 6, 2016 at 19:17
  • After updating the constraint 'containerViewHeightConstraint.constant = tableView.contentSize.height' you must call 'self.view.layoutIfNeeded()' which layouts the subviews with updated constraints of the views. Jul 6, 2016 at 21:07
  • is that only if you are using auto layout constraint ? Jul 6, 2016 at 23:17
  • No it actually tells system to layout and redraw the view and its subviews without waiting the Update cycle synchronously. When the call to this method is complete, the layout has already been adjusted and drawn based on all changes that had been noted prior to the method call. Jul 7, 2016 at 2:41
  • so where exactly would i put this Jul 7, 2016 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.