swift - 将数组计数分配给 tableviewcell 标签

标签 swift uitableview uilabel

我有一个帖子数组,我从一个 PHP 文件返回所有帖子。我正在尝试将帖子数分配给 tableview 单元格中的标签。但是,当我将帖子数分配给标签时,我得到的是零值。有人能帮忙吗?

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 
    } 
}

最佳答案

看起来您正在从 PHP 文件异步获取 posts,在获取它们之后,您只重新加载第二部分。

关于swift - 将数组计数分配给 tableviewcell 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46039343/

相关文章:

swift - 如何在 UITableViewCell 中显示 FBSDKLoginButton?

ios - 如何使用省略号自动截断单词边界上的 UILabel?

ios - 在一个标签中有不同的颜色和不同的字体大小

php - 使用 swift 解析 json 数据时出错 "The data couldn’ t 被读取,因为它的格式不正确。”

arrays - 如果属性值存在于另一个对象数组中,则快速过滤对象数组

ios - 带参数的 Alamofire 图像上传和带补丁方法的 AuthToken header

iphone - 如何设置 uilabel 的高度等于 uitableviewcell

ios - 阴影未显示在 Collection View 中

ios - sectionHeader 中的自动调整字体大小

iphone - 显示 Unicode 字符的 UILabel