Swift:Dynamicall将JSON添加到表格单元格

时间:2017-02-04 05:30:40

标签: ios arrays json swift

我有这个代码,它抓取了JSON信息

struct cellData {
    let cell : Int!
    let text : String!
    let image : UIImage!
}

(struct在控制器类之前处于顶层)

var arrayData = [cellData]()

覆盖func viewDidLoad(){

    print(annotationId)
    print(annotationTitle)

    arrayData = [
        cellData(cell : 0, text : annotationTitle, image: #imageLiteral(resourceName: "ln-bkg"))
    ]

    let pumpsURL = "http://www...." + annotationId

    Alamofire.request(pumpsURL).responseJSON { response in

        if response.result.value != nil {
            NSLog("Success")
            let pumpsJSON = JSON(response.result.value!)

            let allPumps = pumpsJSON["Pumps"]
            let number = allPumps.count

            for i in 1..<number {
                let pumpName = pumpsJSON["Pumps"][i]["name"].stringValue
                self.pumpNames.append(pumpName)

                self.arrayData.append(cellData(cell : i, text : pumpName, image: #imageLiteral(resourceName: "ln-bkg")))

            }
            print(number)
            print(self.arrayData) // works

        }
    }

}

这是将数组中的数据添加到单元格中的函数:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch (arrayData[indexPath.row].cell) {
    case 0:
        let cell = Bundle.main.loadNibNamed("FirstCell", owner: self, options: nil)?.first as! FirstCell
        cell.firstImage.image = arrayData[indexPath.row].image
        cell.firstCellLabel.text = arrayData[indexPath.row].text
        cell.backgroundColor = UIColor.clear

        return cell

    case 1...10:
        let cell = Bundle.main.loadNibNamed("StationPumpCell", owner: self, options: nil)?.first as! StationPumpCell
        cell.pumpImage.image = arrayData[indexPath.row].image
        cell.pumpLabel.text = arrayData[indexPath.row].text

        return cell

    default:
        let cell = Bundle.main.loadNibNamed("FirstCell", owner: self, options: nil)?.first as! FirstCell
        cell.first.image = arrayData[indexPath.row].image
        cell.firstCellLabel.text = arrayData[indexPath.row].text

        return cell
    }

所有这一切都很好。当我想添加导致错误的变量时。 问题是a)

cellData(cell : 0, text : annotationTitle, image: #imageLiteral(resourceName: "ln-bkg")),

不会回显到单元格(也尝试self.annotationTitle),即使它打印出来。我也不能添加pumpName修正了我的错误,它在另一个ViewController中被注释掉了,它也没有打印,我的不好。

问题b)我无法弄清楚如何追加cellData,即

cellData.append[(cell : 3, text : pumpName, image: #imageLiteral(resourceName: "ln-bkg"))]

我已经尝试并得到错误'无法将值转换为预期的参数'。 已修复在评论中

a)向数组添加数据的正确方法是什么?b)用数组中的变量替换“A title”等硬编码值? IE浏览器。从一个空数组开始,用数据附加单元格0,然后在for循环中附加单元格1-X和数据,为allPumps中的每个JSON对象创建一个单元格(单元格#,文本,图像)(通过放置array.append进入for循环)。

编辑:现在案例0:工作正常,案例1 ... 10:返回空白单元格。当Cell 1 ... X被硬编码时,这工作得更早。 从debug:

打印(self.arrayData)
[Project.cellData(cell: 0, text: Works, image: <UIImage: 0x174096120>, {375, 667}), Project.cellData(cell: 1, text: Success, image: <UIImage: 0x1702855a0>, {375, 667}), Project.cellData(cell: 2, text: Success, image: <UIImage: 0x17009a090>, {375, 667})]

1 个答案:

答案 0 :(得分:1)

在追加数组中的所有tableView后,您需要重新加载data。还要在主线程上重新加载。

Alamofire.request(pumpsURL).responseJSON { response in

    if response.result.value != nil {
        NSLog("Success")
        let pumpsJSON = JSON(response.result.value!)

        let allPumps = pumpsJSON["Pumps"]
        let number = allPumps.count

        for i in 1..<number {
            let pumpName = pumpsJSON["Pumps"][i]["name"].stringValue
            self.pumpNames.append(pumpName)

            self.arrayData.append(cellData(cell : i, text : pumpName, image: #imageLiteral(resourceName: "ln-bkg")))

        }
        //Reload the tableView
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

注意:您已使用for loop启动1..<number,因此您忽略了数组的第一条记录,因为数组以0索引开头。