【发布时间】: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()”。