ios – 自动刷新tableview,无需刷新

ios – 自动刷新tableview,无需刷新,第1张

概述我想知道如何自动刷新tableview而不必下拉刷新.所以我尝试设置NSTimer并调用具有reloadData()的函数.但那没用.换句话说,我做了: @IBOutlet weak var allPrayerRequestsTableView: UITableView!var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target 我想知道如何自动刷新tablevIEw而不必下拉刷新.所以我尝试设置NSTimer并调用具有reloadData()的函数.但那没用.换句话说,我做了:

@IBOutlet weak var allPrayerRequeststableVIEw: UItableVIEw!var timer = NSTimer.scheduledTimerWithTimeInterval(0.4,target: self,selector: "update",userInfo: nil,repeats: true)func update() {    allPrayerRequeststableVIEw.reloadData()}

但这没效果.有人知道如何每隔几秒自动刷新一次tablevIEw吗?

解决方法 尝试以这种方式在主线程中重新加载tablevIEw:

dispatch_async(dispatch_get_main_queue()) {    self.allPrayerRequeststableVIEw.reloadData()}

你的方法将是:

func update() {    dispatch_async(dispatch_get_main_queue()) {        self.allPrayerRequeststableVIEw.reloadData()    }}

示例代码:

import UIKitclass VIEwController: UIVIEwController,UItableVIEwDataSource,UItableVIEwDelegate {    @IBOutlet weak var allPrayerRequeststableVIEw: UItableVIEw!    var tableArray = [Int]()    var count = 0    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        allPrayerRequeststableVIEw.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: "cell")        allPrayerRequeststableVIEw.delegate = self        allPrayerRequeststableVIEw.dataSource = self        var timer = NSTimer.scheduledTimerWithTimeInterval(1,repeats: true)    }    func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int{        return tableArray.count    }    func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell{        var cell:UItableVIEwCell = tableVIEw.dequeueReusableCellWithIDentifIEr("cell") as! UItableVIEwCell        cell.textLabel?.text = "\(tableArray[indexPath.row])"        return cell    }    func update() {        count++        //update your table data here        tableArray.append(count)        dispatch_async(dispatch_get_main_queue()) {            self.allPrayerRequeststableVIEw.reloadData()        }    }}

HERE是最终项目.

总结

以上是内存溢出为你收集整理的ios – 自动刷新tableview,无需刷新全部内容,希望文章能够帮你解决ios – 自动刷新tableview,无需刷新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1030165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存