【问题标题】:UITableView returns blank cellsUITableView 返回空白单元格
【发布时间】:2016-12-27 05:52:24
【问题描述】:

我有一个表格视图,其中单元格的高度设置为自动。 在几次运行中,表格中的一些单元格显示为空(空白)。调试后,我注意到“cellForRowAtIndexPath”函数像往常一样返回这些单元格。此外,每次出现此错误时,控制台都会显示以下消息: “仅警告一次:检测到约束模棱两可地建议 tableview 单元格的内容视图高度为零的情况。我们正在考虑意外折叠并改用标准高度。”

上下滚动几次可以解决问题并显示单元格。

我将以下函数用于高度:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return UITableViewAutomaticDimension

}

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

    return UITableViewAutomaticDimension
}

我已经坚持了一周,任何帮助将不胜感激!

编辑:单元格代码:

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

    return posts.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var row = indexPath.row

    tableView.allowsSelection = false;

    let cellIdentifier = "testTableViewCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! testTableViewCell

    let post: PostMdl!

    if(row < posts.count) {

        post = posts[row]

    }
    else {
        return cell
    }

    cell.label.delegate = self

    cell.label.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue

    cell.label.userInteractionEnabled = true

    cell.brandName.text = post.brandName

    cell.timeStamp.text = post.timeStamp

    cell.brandImg.sd_setImageWithURL(post.brandImgUrl, placeholderImage: UIImage(named: "placeHolder"))


    if let img = post.postImg {
        cell.mainImg.image = img

    } else {

        cell.mainImg.image = UIImage(named:"lazyLoad")

    }

    cell.label.text = post.postTag

    cell.labelDistanceFromImg.constant = 30

    cell.labelDistanceToBtm.constant = 30

    cell.postTag = post.postTag

    cell.socNtwkImg.image = UIImage(named: post.socNtwk)


    return cell
}

【问题讨论】:

  • 我遇到了同样的问题。从原型单元格上的UITextview 中删除高度。你能更新你的完整表格视图代码吗?
  • 我只给出了几张图片的高度。除此之外,它的所有相对约束。我在问题中添加了“cellForRowAtIndexPath”和“numberOfRowsInSection”。还有什么我应该补充的吗?
  • 尝试将容器视图添加到 cell.contentView,将其他标签和图像视图放入此容器视图,并将容器的约束设置为我的答案。如果此建议对您没有帮助,您可以尝试手动动态计算单元格的高度。
  • 我无法修复单元格高度,因为它随图像的高度而变化。我找到了修复。在 cellForRowAtIndexPath 方法中返回单元格之前,我必须添加“cell.layoutSubviews()”。

标签: ios swift xcode


【解决方案1】:

我猜你对 UITableViewCell.contentView 的子视图设置了错误的约束。

这张图片显示了我重现您的错误:

请注意红色视图的右侧约束,它只有左上右高,缺少底部约束,当我添加它时,它看起来不错:

是的,还是有问题不能同时满足约束我们可以修改高度约束的优先级,需要默认优先级(1000),我们改成Hight(750) ,它有效!

我不知道你的单元格的详细信息,但错误的原因很可能,希望我的回答对你有所帮助。

附加代码

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return tableView.dequeueReusableCellWithIdentifier("cell")!;
}


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

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
}

【讨论】:

    【解决方案2】:

    您的 iOS 手机版本是多少? 在之前的iOS8版本中我也遇到过类似的问题。 我的方法如下:

    tableView.estimatedRowHeight = 88.0 tableView.rowHeight = UITableViewAutomaticDimension

    希望能帮到你

    【讨论】:

    • 它的 iOS 9.3。那么将estimatedRowHeight 更改为固定值是解决方法吗?因为我也试过了。没用..
    • 对不起,我帮不了你。
    • 好的。无论如何谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2013-09-25
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多