Swift 4 UITableView单元格高度不起作用
问题描述:
我试图设置Xcode9的高度,除非它看起来不起作用。我可以逃脱的76一个固定大小我有一个形象,在那里,我想有10填充与56Swift 4 UITableView单元格高度不起作用
的高度我已经试过什么: 1.尺寸设置它督察 2.通过编码,但它不工作
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 76
getArticles() // returns json data
}
而且的tableView
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell") as? ArticleCell else { return UITableViewCell() }
cell.titleLabel.text = articles[indexPath.row].title
if let imageURL = URL(string: articles[indexPath.row].image) {
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.imgView.image = image
}
}
}
}
cell.contentView.setNeedsLayout()
cell.contentView.layoutIfNeeded()
return (cell)
}
似乎没有任何工作,并保持在同一高度。我已经尝试过,没有任何限制。
我在这里做错了什么?
编辑 我已经从Prototype cell更改为xib。现在我有它的工作,除了我得到约束的错误。
这是它的唯一工作方式。如果我删除底部边距,它会再次回到较小的尺寸。如果我删除图像高度相同。
2017-09-25 19:26:37.842902+0200 Proj010916-NL_nl[56361:915542] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60c000093c90 UIImageView:0x7fec2800cd60.height == 56 (active)>",
"<NSLayoutConstraint:0x60c000093ce0 UIImageView:0x7fec2800cd60.top == UITableViewCellContentView:0x7fec2800aa70.topMargin + 10 (active)>",
"<NSLayoutConstraint:0x60c000093d80 UITableViewCellContentView:0x7fec2800aa70.bottomMargin == UIImageView:0x7fec2800cd60.bottom + 10 (active)>",
"<NSLayoutConstraint:0x604000097160 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fec2800aa70.height == 92 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60c000093c90 UIImageView:0x7fec2800cd60.height == 56 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
下一个任务是重做赛格,因为它现在是一个vc,而不是一个单元格了。
答
您是否尝试过实施heightForRowAtIndexPath委托方法?
override open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 76.0
}
这样你就可以为UITableView中的每个indexPath设置不同的高度。
+0
我想他试图实现自动调整大小的tableViewCell。约束是关键。 – Glenn
你的代码是正确的。您应该向“ArticleCell”xib文件添加一些约束,添加它们并确保您将内容连接到超级视图的顶部和底部。如果它仍然无法正常工作,请在此问题上发布您的约束,以便我们帮助您解决问题。 –
更新的约束信息。 – GSK1985
阅读日志,你会知道iOS打破了约束条件,恒定的高度56.它与另一个约束条件相冲突。想想看。 – Glenn