【问题标题】:UITableViewCell: get the exact width of UILabel in cellUITableViewCell:获取单元格中 UILabel 的确切宽度
【发布时间】:2016-03-27 16:27:59
【问题描述】:

在自定义单元格中获取正确的标签宽度时遇到问题。 这是我在故事板中的单元格:

创建表格时,我知道调用cellForRowAtIndexPath时,单元格刚刚创建,还没有添加到UITableView中,所以标签的宽度是304px。这会导致如下结果:

滚动后,单元格已添加到 UITableView 中,可以正确显示,标签宽度为 164px。

在添加到 UITableView 之前,有没有办法知道单元格/标签的确切宽度?

标签上的约束:Leading、Trailing、Top 和 Height

以下是源代码:

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
}

【问题讨论】:

  • 尝试子类化 tableview 单元格并在 layoutsubviews 方法或 initWithStyle 方法中找出边界。
  • 我推荐你使用自动布局。
  • 显示您的 cellforRow 代码,是的,使用自动布局..
  • 我当然用过自动布局 :)
  • 我已经在上面添加了我的代码

标签: ios swift uitableview uilabel


【解决方案1】:

就试图获得标签的宽度而言,你有没有单独尝试过

的行
self.YourLabelName.bounds.width

【讨论】:

    【解决方案2】:

    您可以有一个标签,它可以根据使用自动布局显示的文本来调整其高度。

    1.为此,您不应为单元格设置静态高度,只需添加以下两个代表,

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

    2。您应该为您的单元格设置从上到下的约束, 在您的情况下,您的标题标签应该有 TopSpace 到 Cell、LeadingSpace 到 ImageView、TrailingSpace 到 Cell(标准间距)和 BottomSpace 到 AuthorLabel(标准间距)。

    3.您希望根据文本具有动态高度的标签应将行数设置为 0,将 LineBreakMode 设置为自动换行。

    就是这样,也找到与您的情况类似的此链接,

    Dynamic UITableViewCell content does not expand cell

    Dynamically increase height of UILabel & TableView Cell?

    【讨论】:

      【解决方案3】:

      尝试在func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)方法中调用titleLbl.requiredHeight()

      【讨论】:

        猜你喜欢
        • 2011-10-20
        • 1970-01-01
        • 2014-09-07
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 2013-07-21
        • 1970-01-01
        • 2023-03-22
        相关资源
        最近更新 更多