1
我有一个posts数组,我从PHP文件返回所有帖子。我试图将帖子的数量分配给tableview单元格中的标签。但是,当我将帖子的数量分配给标签时,我得到的是零值。有人能帮忙吗?为tableviewcell标签分配一个数组
var posts = [AnyObject]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else {
return posts.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! InfoCell
print(posts.count)
cell.PostsLbl.text = String(posts.count)
return cell
}
}
数组是否从网络请求的响应填充?如果是这样,那么您最有可能在异步网络请求完成执行之前尝试使用该数组。但是,没有更多的上下文,就无法确定。请包括'posts'的声明以及实际修改它的所有代码。 –
我已经包含了帖子的声明。谢谢。 – ILoveToCode22
你是什么意思*“我得到一个零值”*?你在表格视图第一部分的这个'InfoCell'中究竟看到了什么? – rmaddy