gpt4 book ai didi

ios - UICollectionView:更新节页脚标题

转载 作者:行者123 更新时间:2023-11-30 11:09:06 25 4
gpt4 key购买 nike

我使用 UICollectionView 作为 Messenger UI。部分页脚显示发送消息的时间。但是,我只想在上一条消息在 15 分钟前发送时显示它。

因此,我想在每次发送消息时刷新页脚。但是,如何实现节页脚标题的刷新呢?

我尝试了self.collectionView.reloadData(),但是,它不会更新页脚(将它们设置为“”)。 (当我上下滚动时,它们消失,所以逻辑有效。)

你知道如何刷新页脚标题吗?

最佳答案

假设你像这样设置你的collectionView

enter image description here

您正在使用默认的流程布局。这样您就可以向页眉和页脚添加标签。并在页脚标签中,将其标记设置为 1001(或您喜欢的任何数字)。

页眉的标识符是“header”,页脚的标识符是“footer”。UICollectionElementKindSectionHeader 在流布局类中定义(检查文档。)

enter image description here

现在您可以像这样重新加载页眉和页脚:

override  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView{
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: (kind == UICollectionElementKindSectionHeader ? "header": "footer"), for: indexPath)
// Configure the cell
return cell
}

当您尝试更改页脚中标签的内容时,无需刷新页脚。您可以在应用程序中的任何位置调用以下内容。为了方便起见,我将其称为“形成一个单元格来运行测试”.

 override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
let footView = collectionView.supplementaryView(forElementKind: UICollectionElementKindSectionFooter, at: IndexPath.init(row: 0, section: 1))
if let label = footView?.viewWithTag(1001) as? UILabel{
label.text = "AnyThing"
}

这里我使用 IndexPath.init(row: 0,section: 1),这是第二部分。现在您可以看到结果了。

enter image description here

这是一个最简单的例子。 CollectionView 可能非常复杂,对于各种应用程序和示例都值得一本书。希望这可以帮助您。

关于ios - UICollectionView:更新节页脚标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52373945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com