我在我的 Swift 文件中声明了一个字符串数组。
var postTitleArray : [String] = []
在我的 viewDidLoad
方法中,我将值附加到我的数组中...
RestApiManager.sharedInstance.makeGetRequest("/development/vw_posts?filter=parent_term_id%20%3D%20%22%5B82%5D%22", onCompletion: { json in
for result in json["record"].arrayValue
{
let postTitle = result["post_title"].stringValue
print(postTitle)
self.postTitleArray.append(postTitle)
}
})
除此之外,我在 numberOfRowsInSection 中使用数组计数来设置我的 tableView 的行数...
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.postTitleArray.count
}
但是,numberOfRowsInSection 返回了 0,为什么会这样,我已经为我的 postTitleArray 赋值了,谁能帮我看看这个?赞赏...
最佳答案
在调用 numberOfRowsInSection
之前,您的 API 调用可能尚未完成。
在填充了 postTitleArray
之后,尝试在 UITableView
实例上调用 reloadData
。 (我在这里假设你有一个对你的 UITableView
的引用,它叫做 tableView
)
RestApiManager.sharedInstance.makeGetRequest("/development/vw_posts?filter=parent_term_id%20%3D%20%22%5B82%5D%22", onCompletion: { json in
for result in json["record"].arrayValue
{
let postTitle = result["post_title"].stringValue
print(postTitle)
self.postTitleArray.append(postTitle)
}
self.tableView.reloadData()
})
关于ios - 数组有追加,但里面没有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325792/