【发布时间】:2019-06-10 11:36:48
【问题描述】:
我有两种不同的单元格类型,一种用于 cmets,另一种用于回复。我试图将它们呈现在相同的collectionView 中,然后可能像这样对它们进行分组:每个具有特定 id 的评论都有它的回复。然而,任何尝试,我都失败了。
你会怎么做?
private var comments = [Comment]()
private var replies = [Reply]()
var items: [Any] = []
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// let item = items[indexPath.item]
var item = items[indexPath.item]
if item is Comment.Type {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CommentCell.cellId, for: indexPath) as! CommentCell
cell.comment = items[indexPath.item] as? Comment
print(item)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RepliesCell.cellId, for: indexPath) as! RepliesCell
cell.reply = items[indexPath.item] as? Reply
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let item = items[indexPath.item]
if item is CommentCell.Type {
let dummyCell = CommentCell(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 50))
dummyCell.comment = items[indexPath.item] as? Comment
dummyCell.layoutIfNeeded()
let targetSize = CGSize(width: view.frame.width, height: 250)
let estimatedSize = dummyCell.systemLayoutSizeFitting(targetSize)
let height = max(40 + 8 + 8, estimatedSize.height)
return CGSize(width: view.frame.width, height: height)
} else {
let dummyCell = RepliesCell(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 50))
dummyCell.reply = items[indexPath.item] as? Reply
dummyCell.layoutIfNeeded()
let targetSize = CGSize(width: view.frame.width, height: 250)
let estimatedSize = dummyCell.systemLayoutSizeFitting(targetSize)
let height = max(40 + 8 + 8, estimatedSize.height)
return CGSize(width: view.frame.width, height: height)
}
}
}
【问题讨论】:
-
如果我理解正确的话,每个回复都有评论的依赖。如果是,您需要将回复放入 cmets 模型中。
-
是的回复位于具有评论 ID 值的节点中。我已经有一个回复模型,并希望按原样使用它。
-
您能否分享 items 对象内部的内容以及您分配它的位置?
-
请调试代码并在进入collectionviews单元格数函数时检查项目数是否正确
-
那么你需要分享你分配数组的位置以及如何重新加载collectionView。
标签: ios swift uicollectionview uicollectionviewcell