iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 自动刷新tableview,无需刷新大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我想知道如何自动刷新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 UIKit

class 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,无需刷新所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。