当前位置: 首页 > 知识库问答 >
问题:

自调整表视图单元格不工作

宋铭
2023-03-14

我将估计的行高度设置为常量,将行高度设置为 UITableView自动维度,但表视图单元格仍未调整为我的单元格的详细信息文本标签的大小。我哪里出错了?

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.estimatedRowHeight = 130.0
    tableView.tableFooterView = UIView()
    tableView.separatorInset.left = 50
    tableView.registerClass(CommentCellView.self, forCellReuseIdentifier: cellid)
    tableView.rowHeight = UITableViewAutomaticDimension


}

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellid, forIndexPath: indexPath) as! CommentCellView

    return cell
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5

}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 60.0
}

UITableViewCell类:

class CommentCellView: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)

        detailTextLabel?.numberOfLines = 0
        detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

}

共有1个答案

东郭自珍
2023-03-14

设置您的标签(在此示例中称为yourText

 var yourText : UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

以这种方式设置单元格约束,并将setupComponents方法放在此处:

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

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

        setupComponents()

    }

        func setupComponents(){



    self.addSubview(yourText)

    yourText.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 6).active = true
            yourText.centerYAnchor.constraintEqualToAnchor(self.centerYAnchor).active = true
            yourText.widthAnchor.constraintEqualToConstant(110).active = true

    yourText.numberOfLines = 0

    yourText.lineBreakMode = NSLineBreakMode.ByWordWrapping
     self.bottomAnchor.constraintEqualToAnchor(yourText.bottomAnchor, constant: 5).active = true
    }
 类似资料:
  • 如何自我调整uItableview单元格元素。 自动调整UITableViewCell大小。

  • 问题内容: 在我的应用程序中,我有一个表格视图,每个单元格中都有一个图像,标签和文本视图。我希望能够根据文本视图中的内容量自动调整单元格的大小。(文本视图是最下面的文本。) 到目前为止,我已经添加了正确的约束。文本视图的前导,尾随,顶部和底部,并且已禁用滚动和编辑。 在我的tableViewController.swift文件中,我编写了以下代码: 但是,这不起作用,因为当我在文本视图中添加更多文

  • 晚上好, 我有以下设计: < li >红框:是表格行 < li>orane box:是一个集合视图 < li >蓝框:是一个采集单元 当集合视图为空时,我希望该行或集合视图自动调整为0。 目前我得到了这个: 所以当没有蓝色框时,我想让橙色框自动调整为高度= 0。 我知道我应该使用collectionView高度的约束,并在为空时设置为0。 但是我在表格单元格中有出口,在视图控制器中有集合委托。我不

  • 我正在使用带有Autolayout和UICollectionView的自动调整单元格。 我可以在单元初始化代码中指定约束: 但是,应用会崩溃,因为单元格尚未添加到。 问题 > 在生命周期的哪个阶段,可以添加具有 的< code>width集合视图< code >的widthUIScreenUIWindow '的实例吗? 编辑问题不是重复的,因为它不是关于如何使用自动调整单元格大小功能,而是关于在使

  • 我在UIViewController中有一个UITableView作为视图的一部分。我正在尝试根据文本的长度自动调整表格单元格的大小。它不会设置textview的文本,单元格将以idCellTextView单元格的标准高度结束。我已经四处搜索过,正在尝试从单元格内的文本视图相对于内容视图和代码在自动布局中使用固定,如下所示: 与问题相关的视图控制器的其余部分显示在以下位置:

  • 我在tableViewController中使用自动大小的表格视图单元格。我已将下面的两行添加到viewDidLoad(),执行正确的自动布局,并将带有扩展文本的标签的Lines属性更改为0。 但是,当视图第一次加载时,单元格不会自动调整大小。但是如果我向下滚动或者翻转到横向视图,然后再翻转回来,自动调整大小就能完美地工作。所以我添加了下面的代码来查看WillAppear函数: 现在,当table