0

I am having the problem when getting the correct width of the label in custom cell. This is my cell in storyboard: cell in storyboard

When the table is created, I understand that when cellForRowAtIndexPath is called, the cell is just created and not added to UITableView yet, so the width of the label is 304px. This causes the result like this:

Wrong result

After scrolling, the cell has been added to the UITableView so it shows correctly, with the width of the label is 164px.

Correct result

Is there any way to know the exact width of cell/label before it is added to UITableView?

The constraints on the label: Leading, Trailing, Top and Height enter image description here

Below is source code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: ArticleCell = tableView.dequeueReusableCellWithIdentifier("articleCell", forIndexPath: indexPath) as! ArticleCell
    let item = array.objectAtIndex(indexPath.row) as! Article
    cell.showDataForArticle(item, dateFormatter: dateFormatter)
    return cell
}

func showDataForArticle(article: Article, dateFormatter: NSDateFormatter) {
    self.titleLbl.text = article.articleTitle
    titleHeightConstraint.constant = titleLbl.requiredHeight() <--- where problem happens

    self.thumbnailImgView.imageLink(article.articleThumbnailLink!)
    self.authorLbl.text = article.articleCreator
    self.dateLbl.text = dateFormatter.stringFromDate(article.articlePubDate!)
}


extension UILabel {
func requiredHeight() -> CGFloat {
    let frame = CGRectMake(0, 0, self.frame.size.width, CGFloat.max)
    let label: UILabel = UILabel(frame: frame)
    label.numberOfLines = 0
    label.lineBreakMode = .ByWordWrapping
    label.font = self.font
    label.text = self.text
    label.sizeToFit()

    return label.frame.size.height
}
haisergeant
  • 1,111
  • 2
  • 13
  • 32

3 Answers3

0

Try calling titleLbl.requiredHeight() in func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) method

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Asad Khan
  • 365
  • 3
  • 7
0

As far as trying to get the width of the label have you tried something alone the lines of

self.YourLabelName.bounds.width
Dallas
  • 1,788
  • 2
  • 13
  • 24
0

You can have a label that would adjust its height based on the text it has to show using auto layouts.

1. For this to work you should not set static height for your cell, just add the below two delegates,

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 44;
    }

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       return UITableViewAutomaticDimension;
    }

2. You should set top to bottom constraint for your cell, In your case your header label should have TopSpace to Cell, LeadingSpace to ImageView, TrailingSpace to Cell (with standard spacing) and BottomSpace to AuthorLabel(with standard spacing).

3. The label you want with dynamic height as per text should have set the number of lines to 0 and LineBreakMode to Word Wrap.

Thats it, also find this links similar to your case,

Dynamic UITableViewCell content does not expand cell

Dynamically increase height of UILabel & TableView Cell?

Community
  • 1
  • 1
Bharat Modi
  • 4,158
  • 2
  • 16
  • 27