ios - 数组有追加,但里面没有值

标签 ios arrays swift uitableview rest

我在我的 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/

相关文章:

ios - 当有人滚动超出 UICollectionView 的顶部时,我如何调用方法?

swift - Segue "show detail"在 iOS 7 中不起作用 - Swift

ios - Facebook 上的视频分享

ios - ImagePicker 问题并分配给 ImageView

PHP比较数组

arrays - Perl 中的哈希变量和数组给出了一个尴尬的输出

arrays - 从 Json Angular 5 插入数组

swift - 调整 UITableView 的大小

ios - SpriteKit SKVideoNode 未出现在屏幕上,但可以听到声音

ios - 位置更新的时间间隔更短