将数据打印到 TableView 时出现问题 (Swift)
Problems with printing data into TableView (Swift)
我有一个函数可以发出 http 请求并将结果保存到 2 个数组中
然后我调用 tableViewObejct.reloadData()
将结果显示到 tableView
。但是如果我在第一个函数之后执行第二个 - 什么都不会发生。我必须将我的函数调用到 viewDidLoad
,然后我有一个我正在做的按钮 reloadData()
,只有在那之后我才能在 tableView
.[=24= 中看到我的数据]
函数:
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
是全局 public 字符串数组,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 函数并同时在 tableView
中查看结果的问题,只需通过 viewDidLoad
或单击按钮即可。为什么我必须将这些操作分开才能在 tableView
中查看数组数据?
当您从服务器获取数据时,尝试以这种方式重新加载您的 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()
}
}
}
这个问题有点难以理解,但我认为发生的情况是,在您调用 tableView.reloadData()
时,您的 GET
请求尚未完成。现在我不知道 httpGet
函数的内部实现,但我会假设它是异步的。这意味着您的表视图在请求完成之前正在读取数据。
我有一个函数可以发出 http 请求并将结果保存到 2 个数组中
然后我调用 tableViewObejct.reloadData()
将结果显示到 tableView
。但是如果我在第一个函数之后执行第二个 - 什么都不会发生。我必须将我的函数调用到 viewDidLoad
,然后我有一个我正在做的按钮 reloadData()
,只有在那之后我才能在 tableView
.[=24= 中看到我的数据]
函数:
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
是全局 public 字符串数组,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 函数并同时在 tableView
中查看结果的问题,只需通过 viewDidLoad
或单击按钮即可。为什么我必须将这些操作分开才能在 tableView
中查看数组数据?
当您从服务器获取数据时,尝试以这种方式重新加载您的 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()
}
}
}
这个问题有点难以理解,但我认为发生的情况是,在您调用 tableView.reloadData()
时,您的 GET
请求尚未完成。现在我不知道 httpGet
函数的内部实现,但我会假设它是异步的。这意味着您的表视图在请求完成之前正在读取数据。