Set tableview cell row height dynamically?

2019-07-13 09:11发布

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?

3条回答
forever°为你锁心
2楼-- · 2019-07-13 09:29

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:

  1. 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.

  2. 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).

  3. 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.

查看更多
三岁会撩人
3楼-- · 2019-07-13 09:40

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

查看更多
甜甜的少女心
4楼-- · 2019-07-13 09:52

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)
        }
}
查看更多
登录 后发表回答