Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 修改行数组时,Swift 2数据不会重新加载数据_Ios_Arrays_Json_Swift_Swift2 - Fatal编程技术网

Ios 修改行数组时,Swift 2数据不会重新加载数据

Ios 修改行数组时,Swift 2数据不会重新加载数据,ios,arrays,json,swift,swift2,Ios,Arrays,Json,Swift,Swift2,我刚刚开始学习iOS开发和Xcode,我正在学习Swift 2。我试图从URL获取一些JSON数据,将其拆分为一个swift数组,并将其显示在表视图中。我已经成功地将JSON数据拆分为一个数组,但是我在重新加载表的数据以使其显示此数据时遇到了问题。以下是我的代码: // // ViewController.swift // Table JSON // // Created by James Allison on 06/11/2015. // Copyright © 2015 James

我刚刚开始学习iOS开发和Xcode,我正在学习Swift 2。我试图从URL获取一些JSON数据,将其拆分为一个swift
数组
,并将其显示在
表视图中。我已经成功地将JSON数据拆分为一个
数组
,但是我在重新加载表的数据以使其显示此数据时遇到了问题。以下是我的代码:

//
//  ViewController.swift
//  Table JSON
//
//  Created by James Allison on 06/11/2015.
//  Copyright © 2015 James Allison. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var cellContent = ["this should be replaced","something","something else"]

    @IBOutlet weak var table: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // construct url
        let url = NSURL(string: "http://127.0.0.1:8888/json.php")!

        let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
            // the following will happen when the task is complete
            if let urlContent = data {

                var webContent =  NSString(data: urlContent, encoding: NSUTF8StringEncoding)

                // remove []
                webContent = webContent?.stringByReplacingOccurrencesOfString("[", withString: "")
                webContent = webContent?.stringByReplacingOccurrencesOfString("]", withString: "")

                // split by commas
                var webContentArr = webContent?.componentsSeparatedByString(",")

                var temp = ""
                // remove quote marks
                for var i = 0; i < webContentArr!.count; i++ {
                    temp = webContentArr![i]
                    temp = temp.stringByReplacingOccurrencesOfString("\"", withString: "")
                    webContentArr![i] = temp
                }

                print(webContentArr!)
                self.cellContent = webContentArr! as! Array
                self.table.reloadData()

            }
            else {
                // something failed
                print("Error: invalid URL or something.")
            }
        }
        task.resume()

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellContent.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        cell.textLabel?.text = cellContent[indexPath.row]

        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
猜测一下,您的“任务完成时将发生”代码正在一些线程/队列上运行,而不是在main上运行,这对UI更新不起作用。任何涉及UI的操作都必须在主队列上进行

您应该调整代码,以便在处理完所有内容后,在主队列上安排对
cellContent
的替换和对表视图的调用
reloadData()
。为此,将上述两个调用包装在发送到主队列的异步调度中:

dispatch_async(dispatch_get_main_queue(), ^{

    self.cellContent = webContentArr! as! Array
    self.table.reloadData()

});
这将确保
cellContent
数组在更新主队列上的UI时不会“在表视图背后”被修改(糟糕!),并且表视图在完成任何正在进行的更新之前不会再次尝试更新

我希望这有帮助。

不客气。:-)这是一个非常重要的主题,尤其是在与任何UI对象交互时。每当您传递一个需要在运行UI时更新UI的闭包(稍后运行的代码块)时,请记住“跳回到”主队列,以更新UI所需的任何集合(如
cellContent
array)或其他属性,并更新UI本身。
dispatch_async(dispatch_get_main_queue(), ^{

    self.cellContent = webContentArr! as! Array
    self.table.reloadData()

});