Reputation: 683
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.
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?
Upvotes: 0
Views: 5110
Reputation: 288
[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;
Upvotes: 0
Reputation: 8324
I have 2 solutions for your problem:
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.
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.
Upvotes: 2