如何以编程方式将自定义单元格插入TableView

时间:2014-11-27 23:43:38

标签: ios xcode uitableview swift

我正在尝试将自定义单元格插入到我的表格视图中,其中包含从Web请求加载的数据,但是在插入数据之前,表格插入脚本正在运行,最后会出现一个空白表格。

目前我的代码是这样的,我已经测试了它以处理虚拟数据:

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        mainTableView.estimatedRowHeight = 50.0
        mainTableView.rowHeight = UITableViewAutomaticDimension
        var cell : postWithTitleAndImageTableViewCell = mainTableView.dequeueReusableCellWithIdentifier("postWithTitleAndImage") as postWithTitleAndImageTableViewCell
        let image = UIImage(named: "chainz on deck.jpg")
        var id = json[indexPath.row]["id"] as String
        var title = json[indexPath.row]["name"] as String
        var text = json[indexPath.row]["post"] as String
        var time = json[indexPath.row]["time"] as String
        var popularity = json[indexPath.row]["popularity"] as String
        //add clientid
        cell.setCell(id, title: title, image: image!, text: text, time: time, popularity: popularity)
        return cell as postWithTitleAndImageTableViewCell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)! Which the ID is: \(readIDs[indexPath.row])")
    }

    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return json.count
    }

代码完全符合它的预期,除了json变量是从Web请求加载的,并且表在实际加载数据之前尝试插入。我该怎么解决这个问题?有没有办法要求手动插入自定义单元格,还是有办法在加载数据后调用tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)函数?我该怎么办?

1 个答案:

答案 0 :(得分:0)

使用mainTable.reloadData()工作。谢谢@Paulw11

相关问题