gpt4 book ai didi

ios - 如何每秒更新一次 UICollectionViewCell 的 UILabel (swift)

转载 作者:行者123 更新时间:2023-11-29 00:47:41 27 4
gpt4 key购买 nike

我有一个 UICollectionViewCell 和一个 UILabel 显示两个日期之间的日差。我想每秒更新一次。我曾尝试使用 NSTimer 重新加载 UICollectionview 的数据,但它使 UI 非常慢。

我该如何实现这一目标?谢谢。

最佳答案

您不必重新加载整个 CollectionView 甚至单个单元格来更新标签。

你有两个选择:

  1. 在ViewController中编写一个NSTimer,只刷新可见单元格的内容。在 ViewController 中每秒安排一个重复计时器。

当定时器触发后,再通过以下方法获取可见单元格,只获取显示时间的标签。

    let visibleIndexPaths = tableview.indexPathsForVisibleRows

var visibleCellsArray: [UITableViewCell] = []

for currentIndextPath in visibleIndexPaths! {
// You now have visible cells in visibleCellsArray
visibleCellsArray.append(tableview.cellForRowAtIndexPath(currentIndextPath)!)
}
  1. 在每个单元中安排一个 1 秒的重复计时器。当定时器被触发时,只需更新相应单元格的标签即可。

关于ios - 如何每秒更新一次 UICollectionViewCell 的 UILabel (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38451793/

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