ios - 如何在 swift 4 的 Collection View 中刷新数据?

标签 ios swift uicollectionview swift4

我有一个函数,每2秒刷新一次数组的数据

var timer = Timer()

func timeRefresh(){
    timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.refreshData), userInfo: nil, repeats: true)

}

我从邮政服务接收数据

@objc func refreshData(){

    post(postString, Route) { (res) in

        let success = res["success"]

        if success == true {
            let walkers = res["walkers"]

            for secondItem in walkers.array! {
                let duration = secondItem["duration"]

                self.timeCar.append(duration.stringValue) //this is the info that i need for the collection view

                }
            }

            print("array time \(self.timeCar)")

        }else{
            self.timeCar = ["-.-","-.-","-.-","-.-","-.-"]
        }
    }
}

我需要每秒刷新一次 Collection View

cell.typeLabel.text = timeCar[indexPath.row]

最佳答案

UICollectionView有多种重新加载数据的方式。

如果你想重新加载每个单元格: collectionView.reloadData() .

如果您想要重新加载特定部分:collectionView.reloadSections(<Array of Sections>) .

如果要重新加载特定单元格:collectionView.reloadItems(<Array of IndexPaths>) .

关于ios - 如何在 swift 4 的 Collection View 中刷新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055577/

相关文章:

ios - DispatchQueue.main.asyncAfter 带开/关开关

ios - 通过滑动显示单个单元格的 UICollectionView 来触发函数

ios - xcode 调试器说由于 swift 4 中的内存问题而终止

ios - 如何通过 NSDictionary 中的键 :value, 过滤 NSArray

ios - 如何创建向用户显示一堆卡片的 Collection View ?

ios - UICollectionViewController 以编程方式因非零布局参数错误而崩溃

ios - Xcode 不使用 IOS 8 SDK 合成属性

ios - 如何在 Objective-C 中将 AVAudioPCMBuffer 转换为 NSData?

swift - 有没有办法获得 ping 的结果,显示在文本框中,但没有旋转的色轮?

ios - 重新加载后 uicollectionviewcell 标签未对齐