【问题标题】:Can't get UITableViewAutomaticDimension to work properly无法让 UITableViewAutomaticDimension 正常工作
【发布时间】:2017-01-26 18:46:12
【问题描述】:

我有一个相当标准的 UITableView,它通过自定义单元格填充。该单元格目前只是一个图像和一个标签。我一辈子都不能让它自己调整大小。

当我包含 UITableViewAutomaticDimension 时,除了不正确的布局外,我无法填充数据。

没有UITableViewAutomaticDimension,数据显示正常。

am使用SnapKit 处理约束和Meteor/SwiftDDP 处理数据,但项目中还有另一个UITableView 似乎工作正常

视图控制器

class CommentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var commentTable:UITableView!
    var comments:MeteorCollection<Comment>!

    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        comments = MeteorCollection<Comment>(name: "comments")

        createView()

        Meteor.subscribe("postsComments", params: [["postId": self.source!.id!]]) {}
    }

    func createView() {
        let contentTableView = UITableView(frame: content.frame)
        content.addSubview(contentTableView)
        contentTableView.backgroundColor = UIColor.clearColor()
        self.commentTable = contentTableView
        contentTableView.delegate = self
        contentTableView.dataSource = self

        contentTableView.snp_makeConstraints { (make) -> Void in
          make.top.equalTo(content)
          make.left.equalTo(content)
          make.right.equalTo(content)
          make.height.equalTo(content).inset(65)
        }

        contentTableView.rowHeight = UITableViewAutomaticDimension
        contentTableView.estimatedRowHeight = 350

    }
}

CommentTableViewDelegate.swift

import UIKit

extension CommentViewController {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.comments.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CommentTableViewCell.reuseIdentifier, forIndexPath: indexPath) as UITableViewCell

        cell.setNeedsUpdateConstraints()
        cell.updateConstraintsIfNeeded()
        if let card = cell as? CommentTableViewCell {
            let item = self.comments.sorted[indexPath.row]
            card.populate(item)
        }

        return CommentTableViewCell()
    }

    func reloadTableView() {
        self.commentTable.reloadData()
    }
}

使用 UITableViewAutomaticDimension 时出现乱码的示例

使用 UITableViewAutomaticDimension 时出现乱码的示例

【问题讨论】:

  • please set contentTableView.estimatedRowHeight = 1 //估计值如果小于则正常工作

标签: swift2 snapkit


【解决方案1】:

这可能是由于单元格内的约束不当造成的。请在表格视图单元格中正确添加约束,并从情节提要的属性检查器部分设置 UILable 的这两个属性:

  • 行到 0
  • 换行换行

或者您也可以从代码中设置这些属性:

 self.lblxyz.numberOfLines = 0
 self.lblxyz.lineBreakMode = .byWordWrapping

注意 - 不要固定 UILable 的高度。

希望对您有所帮助... :)

【讨论】:

    【解决方案2】:

    我不确定它是否有效,但是我最近遇到了同样的问题,我通过更改 estimatedRowHeight 来修复它。

    请您尝试一次:-

    contentTableView.estimatedRowHeight = 350
    

    到,contentTableView.estimatedRowHeight = 160

    【讨论】:

    • 这没有成功。你把你的估计行高度线放在哪里?也许他们和我的不一样。
    • 您确定约束条件吗?是的,我一直在通过创建 tableviewtableview.estimatedRowHeight while viewDidAppear 的插座来使用此功能。
    猜你喜欢
    • 2015-12-17
    • 2015-10-20
    • 2012-09-05
    • 2017-11-06
    • 2011-09-06
    • 1970-01-01
    • 2023-03-10
    • 2020-10-16
    • 2014-01-15
    相关资源
    最近更新 更多