如何每秒更新 UICollectionViewCell 的 UILabel (swift) [英] How to update UICollectionViewCell's UILabel every second (swift)

查看:149
本文介绍了如何每秒更新 UICollectionViewCell 的 UILabel (swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UICollectionViewCell with a UILabel showing the day difference between two dates. I want to update it every second. I have tried using NSTimer to reload the data of the UICollectionview, but it makes the UI very slow.

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

How do I go about achieving this? Thanks.

推荐答案

您不会为了更新标签而重新加载整个 CollectionView 甚至单个单元格.

You do not reload the whole CollectionView or even the individual cells just to update the label.

您有两个选择:

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

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

When the timer is triggered, then obtain the visible cells by the following method and obtain just the label displaying the time.

    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 秒的重复计时器.当定时器被触发时,只需更新相应单元格的标签即可.

这篇关于如何每秒更新 UICollectionViewCell 的 UILabel (swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆