我有表格视图,并从xib取得了自定义单元格,我有很多有媒体或更大的文字时,单元格中要扩展的事物的数量在委托方法中以编程方式管理了高度以及单元格中的约束,但是有时候我打开应用程序加载数据时,单元格会重叠,但是当我上下滚动时,看起来不错。我该怎么做才能在之后立即更新从API获取数据时重新加载?
我认为代码中没有问题,因为它在再次刷新或滚动表视图后才起作用。高度委托方法如下:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let newsDict = self.tableDataNewsArray[indexPath.section]
let newsDataArray = newsDict["news"] as! [NewsBriefModel]
let newsObject = newsDataArray[indexPath.item]
if (self.moreExpandableArray.firstIndex(of: newsObject.newsbrief_id) != nil) {
return 62 + 58 + 4 + 62 //(62:// label minimum height )(46: other than label caponents in cell :: 4: spacing
// 62: more/less button view height)
}else if (self.lessExpandableArray.firstIndex(of: newsObject.newsbrief_id) != nil) {
var baseMoreHeight : CGFloat = newsObject.labelHeightForPostText + 58 + 4 + 62 //(46: other than label caponents in cell :: 4: spacing
// 62: more/less button view height)
if newsObject.numberOfImagesInPost > 0 {
baseMoreHeight = baseMoreHeight + 110
}
if newsObject.numberOfDocumentInPost > 0 {
baseMoreHeight = baseMoreHeight + CGFloat((newsObject.numberOfDocumentInPost * 45))
}
if newsObject.numberOfVideosInPost > 0 {
baseMoreHeight = baseMoreHeight + CGFloat((newsObject.numberOfVideosInPost * 100))
}
return baseMoreHeight
}
}
我在cellForRow中调用以更改UI的Cell方法:
// MARK: CELL METHODS
func showNormalView() {
self.postTitleLabel.numberOfLines = 3
self.postTitleTextView.textContainer.maximumNumberOfLines = 3
self.postTitleTextView.textContainer.lineBreakMode = .byTruncatingTail
self.mediaViewHeight.constant = 0
self.documentTableViewHeight.constant = 0
self.videoTableViewHeight.constant = 0
self.multiMediaView.isHidden = true
self.loadImages(urlString: [])
self.updateConstraintsIfNeeded()
self.setNeedsLayout()
self.layoutIfNeeded()
}
func showMoreView() {
self.postTitleLabel.numberOfLines = 3
self.postTitleTextView.textContainer.maximumNumberOfLines = 3
self.postTitleTextView.textContainer.lineBreakMode = .byTruncatingTail
self.mediaViewHeight.constant = 50
self.imageGridViewHeight.constant = 0
self.documentTableViewHeight.constant = 0
self.videoTableViewHeight.constant = 0
self.loadImages(urlString: [])
self.multiMediaView.isHidden = false
self.updateConstraintsIfNeeded()
self.setNeedsLayout()
self.layoutIfNeeded()
}
这是我使用这种方法所拥有的:
UITableView.automaticDimension
并确保设置了estimatedRowHeight
,此外,请确保该单元格中的最后一个元素的底部约束是ContentView
的Cell