0

How can I change the cell height to make the UILabel fit? I am not using Auto-Layout in my project.

Also, the TableViewCell text is set in the cellForRowAtIndexPath.

Code:

var commentsArray: [String] = []

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

        let cell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell;

        if self.commentsArray.count > indexPath.row{
            cell.commentsText.text = commentsArray[commentsArray.count - 1 - indexPath.row]
            cell.commentsText.font = UIFont.systemFontOfSize(15.0)
        }

        return cell
    }

Any ideas?

Roduck Nickes
  • 1,021
  • 2
  • 15
  • 41
  • take a look at http://stackoverflow.com/questions/19215199/dynamically-size-uitableviewcell-according-to-uilabel-with-paragraph-spacing – Hamish Jan 24 '16 at 21:35
  • @originaluser2 Problem is that the text is already set there, mine is from query. – Roduck Nickes Jan 24 '16 at 21:44
  • `heightForRowAtIndexPath` gets called after `cellForRowAtIndexPath`, so what's the problem? Just calculate the text sizes in `cellForRowAtIndexPath` and return them in `heightForRowAtIndexPath`. – Hamish Jan 24 '16 at 22:03
  • @originaluser2 Like how? – Roduck Nickes Jan 24 '16 at 22:04

2 Answers2

0

The above comment of using heightForRowAtIndexPath is acceptable when not using auto layout, (though I highly recommend getting used it).

Calculating the height of a label when it's populated with a certain string can be done using the methods described in this post: How to calculate UILabel height dynamically?

Community
  • 1
  • 1
orion
  • 354
  • 1
  • 13
  • Hmm, so I added the code on top of the ViewController.swift, and changed my code to this: pastebin.com/MJxPV7JX - But it does not change the size at all..? – Roduck Nickes Jan 25 '16 at 22:18
0

1) You can use a method to set constraints and you can modify the height of the cell in this way: (Example with a label called book_title in a custom UITableViewCell)

fune setupComponents(){

self.addSubview(book_title)

book_title.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: 5).active = true
        book_title.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 10).active = true
        book_title.rightAnchor.constraintEqualToAnchor(self.rightAnchor).active = true
        book_title.widthAnchor.constraintEqualToAnchor(self.widthAnchor, constant: -20).active = true
        book_title.heightAnchor.constraintEqualToConstant(90).active = true
        book_title.numberOfLines = 0
        book_title.lineBreakMode = NSLineBreakMode.ByWordWrapping

}

2) You have to call this method inside these:

 required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        setupComponents()
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: "CellId")

        setupComponents()

    }
Carlo
  • 813
  • 1
  • 15
  • 34