【问题标题】:Problems with printing data into TableView (Swift)将数据打印到 TableView (Swift) 中的问题
【发布时间】:2015-08-05 18:13:58
【问题描述】:

我的函数可以发出 http-request 并将结果保存到 2 个数组中

然后我调用tableViewObejct.reloadData() 将结果显示到tableView。但是,如果我在第一个函数之后执行第二个 - 什么也不会发生。我必须将我的函数调用到viewDidLoad,然后我有一个按钮,我正在执行reloadData(),然后我才能在tableView 中看到我的数据。

功能

func refreshData(){
     self.myData.removeAll(keepCapacity: true)
     self.ids.removeAll(keepCapacity: true)
     httpGet("https://money.yandex.ru/api/categories-list") { result in
         let json=JSON(data: result)
         println(json[0]["subs"].count)
         for var i=0; i<json.count; ++i {
             self.myData.append(json[i]["title"].stringValue)
             self.ids.append("")
             for var g=0; g<json[i]["subs"].count;++g {
                 self.myData.append("    "+json[i]["subs"][g]["title"].stringValue)
                 self.ids.append(json[i]["subs"][g]["id"].stringValue)
             }
         }
         println(self.myData.count)
         println(self.ids.count)
     }
}

myData 是全局公共字符串数组,ids - 也是

tableView 的函数:

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

func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell
{
     let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
     cell.textLabel!.text = myData[indexPath.row]
     return cell
}

所以我需要调用 refreshData-function 并同时查看tableView 的结果,只需通过viewDidLoad 或单击按钮即可。为什么我必须将这些操作分开才能在tableView 中查看数组数据?

【问题讨论】:

    标签: arrays json swift tableview


    【解决方案1】:

    当您从服务器获取数据时,尝试以这种方式重新加载您的 tableView:

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

    你的代码会是这样的:

    func refreshData(){
        self.myData.removeAll(keepCapacity: true)
        self.ids.removeAll(keepCapacity: true)
        httpGet("https://money.yandex.ru/api/categories-list") { result in
            let json=JSON(data: result)
            println(json[0]["subs"].count)
            for var i=0; i<json.count; ++i {
                self.myData.append(json[i]["title"].stringValue)
                self.ids.append("")
                for var g=0; g<json[i]["subs"].count;++g {
                    self.myData.append("    "+json[i]["subs"][g]["title"].stringValue)
                    self.ids.append(json[i]["subs"][g]["id"].stringValue)
                }
            }
            println(self.myData.count)
            println(self.ids.count)
            dispatch_async(dispatch_get_main_queue()) {
                self.tableView.reloadData()
            }
        }
    }
    

    【讨论】:

    • 很高兴为您提供帮助..:)
    【解决方案2】:

    这个问题有点难以理解,但我认为正在发生的事情是,在您致电tableView.reloadData() 时,您的GET 请求尚未完成。现在我不知道httpGet函数的内部实现,但我会假设它是异步的。这意味着您的 tableview 在请求完成之前正在读取数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多