I'm trying to learn Swift and the basics of iOS development at the same time, so bear with me. I have a TableViewController that first parses a local JSON file and displays its very simple data in TableViewCell and SectionHeaderViews. Inside the same TableViewController I have a TableViewController calling the JSON endpoint, which returns the data that I then set for the variables to get access to what I actually want to get to (the API structure is less desirable). So, I finally set the correct data as self.tableData and then called self.tableView.reloadData() but nothing happens. What gives?
import UIKit class BusinessTableViewController: UITableViewController { var data: NSMutableData = NSMutableData() var tableData: NSArray = NSArray() @lazy var Business: NSArray = { let pathTCT = NSBundle.mainBundle().pathForResource("TCT", ofType: "json") let data = NSData.dataWithContentsOfFile(pathTCT, options: nil, error: nil) return NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSArray }() override func viewDidLoad() { super.viewDidLoad() navigationItem.titleView = UIImageView(image: UIImage(named: "growler")) tableView.registerClass(BeerTableViewCell.self, forCellReuseIdentifier: "cell") tableView.separatorStyle = .None fetchKimono() } override func numberOfSectionsInTableView(tableView: UITableView!) -> Int { // return Business.count return 1 } override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { let biz = Business[section] as NSDictionary let results = biz["results"] as NSDictionary let beers = results["collection1"] as NSArray return beers.count } override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as BeerTableViewCell if let path = indexPath { let biz = Business[path.section] as NSDictionary let results = biz["results"] as NSDictionary let beers = results["collection1"] as NSArray let beer = beers[path.row] as NSDictionary cell.titleLabel.text = beer["BeerName"] as String } return cell } override func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String! { let biz = Business[section] as NSDictionary return biz["name"] as String } override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! { let biz = Business[section] as NSDictionary let view = LocationHeaderView() view.titleLabel.text = (biz["name"] as String).uppercaseString return view } override func tableView(tableView: UITableView!, heightForHeaderInSection section: Int) -> CGFloat { return 45 } func fetchKimono() { var urlPath = "names have been changed to protect the innocent" var url: NSURL = NSURL(string: urlPath) var request: NSURLRequest = NSURLRequest(URL: url) var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false) connection.start() } func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) { // Recieved a new request, clear out the data object self.data = NSMutableData() } func connection(connection: NSURLConnection!, didReceiveData data: NSData!) { // Append the recieved chunk of data to our data object self.data.appendData(data) } func connectionDidFinishLoading(connection: NSURLConnection!) { // Request complete, self.data should now hold the resulting info // Convert the retrieved data in to an object through JSON deserialization var err: NSError var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary var results: NSDictionary = jsonResult["results"] as NSDictionary var collection: NSArray = results["collection1"] as NSArray if jsonResult.count>0 && collection.count>0 { var results: NSArray = collection as NSArray self.tableData = results self.tableView.reloadData() } } }
json ios objective-c uitableview swift
chandlervdw Jun 09 '14 at 2:14 2014-06-09 02:14
source share