Swift UITableView: How to Load data from JSON

后端 未结 3 659
一向
一向 2021-02-06 13:35

I am beginner in Swift and would like some help for my first application.I hope that I can explain it well. I am developing and app which is loading information from a JSON File

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 14:11

    You have got the Json Data, Just need to put it in an array and load them into table view like this:

        var tableData:NSArray = []
    
        self.tableData = json["fruits"] as! NSArray
        // In the place of json put your json object
        self.tableView.reloadData()
    
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return self.tableData.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    
        let item = self.tableData[indexPath.row] as! [String : String]
        cell.nameLabel?.text = item["Name"]
        cell.descriptionLabel?.text = item["Description"]
    
        return cell
    }
    

提交回复
热议问题