简体   繁体   中英

How to adjust label height and width in custom tableView Cell

I have a expandable tableView, in which when i expand a section, than there are three cell. On firth Cell there is only name and in second cell. It have a big content. Now I want to auto adjust this label height and width according to content.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tblView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell

        let dataArrayTblView = dataArrayForTableView
        let titleName = dataArrayTblView.valueForKey("name")
        let dueDate = dataArrayTblView.valueForKey("deadlinedate")
        let description = dataArrayTblView.valueForKey("description")

        cell.titleLabel.text = titleName[indexPath.row] as AnyObject! as! String!
        cell.dueDateLabel.text = dueDate[indexPath.row] as? String
        cell.descriptionLabel.text = description[indexPath.row] as? String

        cell.descriptionLabel.sizeToFit()


        cell.textLabel?.backgroundColor = UIColor.clearColor()
        cell.selectionStyle = .None
        return cell
    }

But not getting complete content

在此处输入图片说明

Try to set this. It will automatically adjust the height of the row for you. If it is not working, then you have something wrong with your constraints inside your storyboard.

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Use heightForRowAtIndexPath method to adjust height of row. Calculate size of string with boundingRectWithSize this method. example:

Try This:

if let YOUR_STRING:NSString = str as NSString? {
let sizeOfString = ns_str.boundingRectWithSize(
                                 CGSizeMake(self.titleLabel.frame.size.width, CGFloat.infinity),options: NSStringDrawingOptions.UsesLineFragmentOrigin,   attributes: [NSFontAttributeName: lbl.font], context: nil).size
}

You should use UITableViewAutomaticDimension as row height something like,

  // this should be set in viewDidload
  tableView.rowHeight = UITableViewAutomaticDimension
  tableView.estimatedRowHeight = 140

for that you must use autolayout and should set proper constraints to your label in cell.

Constraint should be in linear chain, I mean your label's top and bottom both constraint must be set and your label should resize according to content so your cell will resize accordingly!

You can refer Self-sizing Table View Cells by Raywenderlich !

Put below in viewDidLoad and set autolayout as per below screenshots.

  tblview.rowHeight = UITableViewAutomaticDimension
  tblview.estimatedRowHeight = 44

Screenshot 1

Screenshot 2

Screenshot 3

Screenshot 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM