简体   繁体   English

UITableView返回空白单元格

[英]UITableView returns blank cells

I have a table view with height of cells set to automatic. 我有一个表格视图,其单元格高度设置为自动。 Once in a few runs, a few cells in the table are displayed empty (white space). 几次运行后,表中的一些单元格将显示为空白(空白)。 Upon debugging, I noticed that the 'cellForRowAtIndexPath' function returns these cells as usual. 调试后,我注意到“ cellForRowAtIndexPath”函数照常返回这些单元格。 Also, every time this bug shows up, the console has this message: "Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead." 此外,每次出现此错误时,控制台都会显示以下消息:“仅警告一次:检测到一种情况,即约束条件模糊地暗示了表视图单元格的内容视图的高度为零。我们正在考虑无意的折叠,而是使用标准高度“。

Scrolling up and down a couple of times fixes the issue and cells are displayed. 上下滚动几次可以解决此问题,并显示单元格。

I use this following functions for height: 我将以下功能用于高度:

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

    return UITableViewAutomaticDimension

}

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

    return UITableViewAutomaticDimension
}

在此处输入图片说明

I have been stuck with this for a week and any help would be greatly appreciated! 我已经坚持了一个星期,任何帮助将不胜感激!

Edit : Code for the cells: 编辑 :单元格的代码:

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
}

I guess you had set wrong constraints to subviews of UITableViewCell.contentView. 我猜您对UITableViewCell.contentView的子视图设置了错误的约束。

This image shows what I reproduce your error: 此图显示了我重现您的错误的内容:

Please note the right constaints of red view, it just has Top Left Right Height , lacks bottom constraint, when I add it and it looks good: 请注意红色视图的右边部分,它只有Top Left Right Height ,没有底部约束,当我添加它时,它看起来不错:

在此处输入图片说明

Yes, there still have problem that Unable to simultaneously satisfy constraints , we can modify the priority of height constraint, default priority is required(1000), we change to Hight(750) , it works! 是的,仍然存在无法同时满足约束的问题我们可以修改高度约束的优先级,默认优先级为required(1000),更改为Hight(750)即可

在此处输入图片说明 在此处输入图片说明

I don't know your cell's detail, but the error's reason is likely, hope my answer helps you. 我不知道您的单元格的详细信息,但可能是错误的原因,希望我的回答对您有所帮助。

Append codes 附加代码

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
}
}

What is your mobile phone version of the iOS? 您的iOS手机版本是什么? In the previous iOS8 version I also encountered similar problems. 在以前的iOS8版本中,我也遇到了类似的问题。 My method is as follows: 我的方法如下:

tableView.estimatedRowHeight = 88.0 tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 88.0 tableView.rowHeight = UITableViewAutomaticDimension

Hope can help you 希望可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM