Reputation: 123
I have a tableview with a label and an image. in some cells, there is no image as i used imageView.removeFromSuperview()
to remove it from the cell. When there is an image in the cell, the row height is 445 and "Custom" is checked off.
how can i set the row height dynamically according to how long the label is instead of how long/big the imageview is after i remove the imageview?
Upvotes: 2
Views: 8943
Reputation: 437632
If you want dynamic row height, you can define your constraints (making sure they're unambiguous), set the label's numberOfLines
to zero, and then in viewDidLoad
, tell it that rows should automatically adjust their height:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
If you want to hide/show a UIImageView
as well, I must confess that I'm not crazy about the removeFromSuperview
approach (because when the cell is reused, you have to re-add the image view, and possibly rebuilding its constraints, too) there are a few options:
You could have a different cell prototype for the cell with the image view and another without the image view. Then cellForRowAtIndexPath
just needs to instantiate the right cell.
You could go ahead and define a full set of constraints that are unambiguous for both the presence of the image and without the image. And then, you can activate
the constraint and set the image view to hidden
as appropriate:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let image = ... // let's say it was an optional, set if needed, left `nil` if not
cell.customImageView?.image = image
if image == nil {
cell.customImageView.hidden = true
cell.imageBottomConstraint.active = false
cell.customLabel.text = ...
} else {
cell.customImageView.hidden = false
cell.imageBottomConstraint.active = true
}
return cell
}
The trick when having competing sets of constraints that dictate the height of the cell is to just make sure that they have different priorities and/or they use inequality so if both sets are in effect, you don't have an unsatisfiable conflict (e.g. the image view might have higher priority).
You can, if you want, go "old school" and programmatically determine the size of the label field, and then implement heightForRowAtIndexPath
, but auto layout makes this process unnecessary.
Upvotes: 8
Reputation: 207
If you're using UILabel, it has a frame with a height property.
And heightForRowAtIndexPath, should cover what you're wanting for the appropriate method to use: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
not sure how your image is currently set up, but here's a rough example:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if image != nil {
return 445.0
else {
label.frame.height (or whichever value you'd prefer)
}
}
Upvotes: 1
Reputation: 996
You need to override this method:
Override func tableview(tableview:UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {}
Return the desired height for the indexPath you want in this method
Upvotes: 1