How to populate my tableview with a mutablearray from json

前端 未结 2 2025
野趣味
野趣味 2021-01-27 10:49

So I\'m fetching data from a url which is in a json format. I\'m trying to display the data in my tableview but, even though it feels simple, I can\'t figure out how to do it.<

2条回答
  •  灰色年华
    2021-01-27 11:02

    Well first change the function to return your company array :

    func getJSON() -> NSMutableArray {
    
    }
    

    By the end of the for loop return the company array

    for company in companies {
    
     }
    

    After your array is populated, return the array inside this block:

    dispatch_async(dispatch_get_main_queue(), { 
    
        return companyArray
    
    })
    

    And after task.resume() return the array:

    return companyArray
    

    From anywhere you wanna call this class and get the array :

    Get a reference of the class

    Let companyModal = CompanyModel()
    

    And in anywhere you have your table view and the class let's say in viewDidLoad, you should first have NSMutableArray.

    var arraySource = NSMutableArray()
    

    And in viewDidLoad :

    arraySource = companyModal.getJSON()
    

    And to show the data in tableView do :

    Mytableview.reloadData()
    

提交回复
热议问题