@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,无需刷新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)