xcode 自动刷新tableview,无需拉动刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32518227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:47:57  来源:igfitidea点击:

Automatic refreshing tableview without pulling to refresh

iosxcodeswiftuitableview

提问by Jae Kim

I want to know how to automatically refresh a tableview without having to pull down to refresh. So I tried setting an NSTimerand calling a function that has reloadData(). But that did not work. In other words, I did:

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

But this did not work. Anybody know how to automatically refresh a tableview every few seconds?

但这没有用。有人知道如何每隔几秒钟自动刷新一个tableview吗?

回答by Dharmesh Kheni

Try to reload your tableview in main thread this way:

尝试以这种方式在主线程中重新加载您的 tableview:

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

And your method will be:

你的方法将是:

func update() {

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

Sample code:

示例代码:

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, target: self, selector: "update", userInfo: nil, 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()
        }
    }
}

回答by Kim Isle Cortez

I believe simply using allPrayerRequestsTableView.reloadData()will not really update or refresh your table view. You will need to update the data source of the table view, then perform the reloadData()method afterwards.

我相信仅仅使用allPrayerRequestsTableView.reloadData()不会真正更新或刷新您的表格视图。您将需要更新表视图的数据源,然后执行该reloadData()方法。

Try this:

尝试这个:

func update() {
    /**
       ADD THE CODE TO UPDATE THE DATA SOURCE
    **/
    allPrayerRequestsTableView.reloadData()
}

回答by John

Did your update gets invoked? If not,a possible reason: normally your app runs at NSDefaultRunLoopMode mode, when you are scrolling your tableview, runloop switches its mode to UITrackingRunLoopMode at which timer is denied, when you stop,runloop returns to default mode.

你的更新被调用了吗?如果不是,一个可能的原因:通常您的应用程序运行在 NSDefaultRunLoopMode 模式下,当您滚动 tableview 时,runloop 将其模式切换到 UITrackingRunLoopMode,在该模式下计时器被拒绝,当您停止时,runloop 返回到默认模式。

So my suggestion is that add your timer to runloop mode kCFRunLoopCommonModes and check if this works.

所以我的建议是将你的计时器添加到 runloop 模式 kCFRunLoopCommonModes 并检查它是否有效。

let timer = NSTimer(timeInterval: 0.4, target: self, selector: "update:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

回答by Efe Helvaci

Make an update method that calls himself again and again, then call it once in ViewDidLoad. For example:

做一个反复调用自己的update方法,然后在ViewDidLoad中调用一次。例如:

-(void)reloadMethod{
    [self.allPrayerRequestsTableView reloadData];

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 4.0 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self reloadMethod];
    });
}

This will update your table view every 4 seconds.

这将每 4 秒更新一次您的表格视图。