数据从全局数组中消失

时间:2017-05-26 11:09:44

标签: ios arrays json swift class

我在父类中定义了一个全局数组,并且我试图让数组填充从REST服务GET调用来自JSON文件的对象。我的想法是我定义了全局数组,填充了从JSON文件填充的对象,然后数据将在我的tableviewcell内部类中使用。但是我注意到数据被填满,它正确地得到json,数据被输入到数组中,但是一切都在方法之外被删除了。在内部类中,数据不存在,甚至在网络请求括号之外。我无法想象这是什么问题。

这是我将数组初始化为全局数组:

class ContentDeliveryViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate{

public var ListOfFiles: [Asset_Content] = []
public var ListOfTags: [String] = []

这是获取json并使用数组填充的方法,其中括号外的print语句表示数组已为空。

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("short CALLED")
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "ContentDeliveryCell", for: indexPath as IndexPath) as! ContentDeliveryCell

    var content: AWSContent
    //fix this
    if(tableView == self.searchDisplayController?.searchResultsTableView){
        print("search part called!!")
        content = filteredList[indexPath.row]
    }else{
        content = contents![indexPath.row]
    }

    if(content.key.contains(IDENTIFIER)){
        //
        let url = URL(string: URL)
        let task = URLSession.shared.dataTask(with: url!){
            data, response, error in guard error == nil else{
                print(error!)
                return
            }
            guard let data = data else{
                print("Data is empty")
                return
            }
            let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [[String: AnyObject]]
            var asset: Asset_Content
            for jay in json! {
                let category = jay["BIGCATEGORY"] as? String
                let diagnosis = jay["DIAGNOSIS"] as? String
                let perspective = jay["PERSPECTIVE"] as? String
                let name = jay["NAME"] as? String
                let title = jay["Title"] as? String
                let UnparsedTags = jay["TAGS"] as? String
                let filename = jay["FILENAME"] as? String

                let tagArray = UnparsedTags?.characters.split(separator: ",")
                for tag in tagArray!{
                    if(self.ListOfTags.contains(String(tag))){

                    }else{
                        self.ListOfTags.append(String(tag))
                        print("TAGCOUNT == ", self.ListOfTags.count)
                    }
                }

                asset = Asset_Content(category!, diagnosis!, perspective!, name!, title!, filename!)
                self.loadJson(forFile: asset)
                print("RightCount === ", self.ListOfFiles.count)// FILLED ARRAY
            }
            print("FirstCount === ", self.ListOfFiles.count)//FILLED ARRAY
        }

        task.resume()
        cell.isHidden = true
        print("FirstCount === ", self.ListOfFiles.count)// EMPTY ARRAY
        print("TAGCOUNT == ", self.ListOfTags.count) // EMPTY ARRAY
        return cell
    }else{
        cell.prefix = prefix
        cell.content = content
        print("KEY" ,content.key)
        cell.isHidden = false
        return cell
    }
}

1 个答案:

答案 0 :(得分:0)

正确的方式:

  1. 加载数据
  2. 解析Json到Objects
  3. 将对象提交到单元格(例如:cell.object = ...)并让单元格自行配置
  4. 重新加载Tableview