【问题标题】:UITableView reload data in SwiftUITableView 在 Swift 中重新加载数据
【发布时间】:2014-06-10 13:38:51
【问题描述】:

Objective-C 中有这个测试项目的代码:

@implementation ViewController {
    NSArray *_locations;
}

- (void)viewDidLoad
{
[super viewDidLoad];
JSONLoader *jsonLoader = [[JSONLoader alloc] init];

NSURL *url = [[NSURL alloc]initWithString:@"http://mechnikova.info/api/pic2.php?task=1"]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    _locations = [jsonLoader locationsFromJSONFile:url];

    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});

在 Swift 中有同一个测试项目的代码:

class ViewController: UITableViewController {
var locations:NSArray=[]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var jsonLoader:JSONLoader = JSONLoader()
    var url = NSURL(fileURLWithPath: "http://mechnikova.info/api/pic2.php?task=1")
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
        self.locations = jsonLoader.locationsFromJSONFile(url)
        self.tableView.performSelectorOnMainThread(selector:(reloadData), withObject: nil, waitUntilDone: true)
    })
}

我有错误 -

中使用了未解析的标识符“reloadData”
self.tableView.performSelectorOnMainThread(selector:(reloadData), withObject: nil, waitUntilDone: true)

请帮忙!

【问题讨论】:

    标签: objective-c uitableview swift ios8


    【解决方案1】:

    用途:

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
            self.locations = jsonLoader.locationsFromJSONFile(url)
            dispatch_async(dispatch_get_main_queue(),{
               self.tableView.reloadData()
            })
        })
    

    【讨论】:

    • 谁说你请看我在iOS9上的以下回答
    • 你是对的,在文档中阅读它并没有被弃用,但是当我尝试运行它时它不可用..
    【解决方案2】:

    为方便起见,您可以使用 Costant

    let diffQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
    let diffMain = dispatch_get_main_queue()
    

    然后在 viewDidLoad() 中使用

    override func viewDidLoad() {
            super.viewDidLoad()
    dispatch_async(diffQueue) {
               self.locations = jsonLoader.locationsFromJSONFile(url)
                    dispatch_async(self.diffMain){
                        self.tableView.reloadData()
                    }
                })
            }
    }
    

    【讨论】:

      【解决方案3】:

      替换你的代码

      self.tableView.performSelectorOnMainThread(selector:(reloadData), withObject: nil, waitUntilDone: true)
      

      有了这个

      self.tableView.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)
      

      差异在于创建选择器

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2014-04-23
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        • 1970-01-01
        • 2014-10-23
        相关资源
        最近更新 更多