【问题标题】:UITableView Dynamic Row Height Without Using AutoLayout不使用 AutoLayout 的 UITableView 动态行高
【发布时间】:2015-12-08 08:10:06
【问题描述】:

我支持 iOS 7,但我没有使用自动布局。有没有办法让单元格标签以这种方式动态高度?

谢谢

编辑:

这是我在 iOS 7 中用于定义动态高度的代码,似乎我可以让它与自动布局一起工作,但它切断了底部的最后一个单元格,这很奇怪。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    var cell = offScreenCells.objectForKey("gcc") as? GalleryCommentCell
    if cell == nil {
        cell = commentsTable.dequeueReusableCellWithIdentifier("GalleryCommentCustomCell") as? GalleryCommentCell
        offScreenCells.setObject(cell!, forKey: "gcc")
    }

    let comment :GalleryCommentInfo = commentResults[indexPath.row]

    setCellCommentInfo(cell!, data: comment)

    cell!.bounds = CGRectMake(0, 0, CGRectGetWidth(commentsTable.bounds), CGRectGetHeight(cell!.bounds))

    cell!.setNeedsLayout()
    cell!.layoutIfNeeded()

    let height = cell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
    return height + 1
}

func setCellCommentInfo(cell :GalleryCommentCell, data :GalleryCommentInfo) { 
    cell.commentDate.text = data.galleryCommentDate

    cell.comment.text = data.galleryComment
}

【问题讨论】:

  • 首先阅读UITableViewDelegate的文档。
  • 我尝试了多种解决方案,但没有任何效果。最后一个单元格似乎被切断了,只是它的底部。我可以发布代码,我什至尝试过使用自动布局。
  • 用您的尝试更新您的问题并解释您遇到的问题。
  • 似乎将 contentInset 添加到我的表格底部解决了中断问题,但不确定这是否是最有效的方法。

标签: uitableview ios7


【解决方案1】:

在您的自定义单元格中实现如下方法:

+ (CGFloat)heightForContactName:(NSString *)name
{
    CGFloat height = 0.0f;
    if (name) {
        CGFloat heightForText;
        // Calculate text height with `textBoundingRect`...
        height += heightForText; 
    }
    return height;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多