0
votes

I have a UITableView inside a regular ViewController and I'd like to adjust the tableview's entire height.

I created the tableview through storyboard, and I'm loading data from an API in my ViewDidLoad. I set up the cells in the typical tableview method.

override func viewDidLoad() {
    self.loadPerformers()
    setupTableView(performersTableView)
}

func setupTableView(tableView: UITableView) {
    tableView.dataSource = self
    tableView.delegate = self
    tableView.estimatedRowHeight = 90
    tableView.rowHeight = UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if tableView == self.performersTableView {
            let performerCell = tableView.dequeueReusableCellWithIdentifier("PerformerCell", forIndexPath: indexPath) as! PerformersTableViewCell
            let performer = self.performersArray[indexPath.row]
            performerCell.performerPic.image = getPerformerImage(performer.english_name)
            performerCell.performerName.text = performer.english_name + "   " + performer.japanese_name
            cell = performerCell
    }
    return cell!
}

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

    tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    tableView.separatorColor = UIColor.clearColor()
    tableView.allowsSelection = false

    if tableView == self.performersTableView {
        count = self.performersArray.count
    }

    return count!

}

The problem I'm having is the tableview stays the same height after loading the data. Below I'm loading four items, but it's only showing 2. The tableview height doesn't change.

enter image description here

I've tried a few things, among them is changing the height of the tableview frame in the viewWillAppear:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    let newHeight = self.performersTableView.rowHeight * CGFloat(self.numberOfPerformers)

    self.performersTableView.frame = CGRectMake(performersTableView.frame.origin.x, performersTableView.frame.origin.y,
        performersTableView.frame.size.width, newHeight)
}

This does nothing though, so I'm misunderstanding something in the rendering process. How do I redisplay the table with my desired height?

2
Is the tableview created in a storyboard/xib? - Lahav
Did you set numberOfRowsInSection to self.performersArray.count ? - Dylan Law
show what you want to achieve, do you want to change the tableview height based on the number of row? or just want to make it look like design by increasing some height? - Tj3n
Yes. I'll add that to my question. It's created in storyboard but I add some setup code I'll add now. - Slamice
If you are talking about adjusting the height of each table view cell, have a look at this. developer.apple.com/library/ios/documentation/UIKit/Reference/…: - Dylan Law

2 Answers

2
votes

I have 2 solutions for your problem:

  1. If you want to set height of Tableview based on Tableview Cells. so first of when you get data from server Reload your Tableview and after reloading tableview you will get content height of Filled tableview using this code:

    self.performersTableView.contentSize.height 
    

    then set this height as your tableview frame.

  2. In design, set fix height of tableview and set reference of constraint and after reloading tableview:

    self.performersTableViewHeightReference.constant = self.performersTableView.contentSize.height
    

I hope this helps You.

0
votes
[self.theTableView setNeedsLayout];
[self.theTableView layoutIfNeeded];

Add this code in your setupTableView and numberOfRowsInSection is data count. And if you want to expend cell also according to content write code in cellForRowAtIndexpath: cell.detailTextLabel.numberOfLines = 0;