Created
September 17, 2015 05:09
-
-
Save tcuongtran/d493555d0d20d6819ac6 to your computer and use it in GitHub Desktop.
Swift 2.0 JSON-TableView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DataTableViewController.swift | |
// tableView | |
// | |
// Created by Cuong Tran on 9/16/15. | |
// Copyright © 2015 Cuong Tran. All rights reserved. | |
// | |
import UIKit | |
class DataTableViewController: UITableViewController { | |
var dataArray: NSArray = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
loadDataFromServer() | |
// Uncomment the following line to preserve selection between presentations | |
// self.clearsSelectionOnViewWillAppear = false | |
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. | |
// self.navigationItem.rightBarButtonItem = self.editButtonItem() | |
} | |
func loadDataFromServer() { | |
let url = NSURL(string: "http://www.reddit.com/.json") | |
let session = NSURLSession.sharedSession() | |
let task = session.dataTaskWithURL(url!, completionHandler: { | |
(data, repsonse, error) -> Void in | |
if (error != nil) { | |
print(error) | |
} else { | |
do { | |
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) | |
if let resultObject = jsonResult as? NSDictionary { | |
if let resultObjectData = resultObject["data"] as? NSDictionary { | |
if let resultObjectChildren = resultObjectData["children"] as? NSArray { | |
dispatch_async(dispatch_get_main_queue(), { | |
self.dataArray = resultObjectChildren | |
self.tableView.reloadData() | |
}) | |
} | |
} | |
} | |
} catch let error as NSError { | |
print(error) | |
} | |
} | |
}) | |
task.resume() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// MARK: - Table view data source | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
// #warning Incomplete implementation, return the number of sections | |
return 1 | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
// #warning Incomplete implementation, return the number of rows | |
return dataArray.count | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) | |
let entry = dataArray[indexPath.row] as! NSDictionary | |
let entryData = entry["data"] as! NSDictionary | |
let picURL = entryData["thumbnail"] as! String | |
let purl = NSURL(string: picURL) | |
let picdata = NSData(contentsOfURL: purl!) | |
if ((picdata) != nil) { | |
cell.imageView?.image = UIImage(data: picdata!) | |
} | |
cell.textLabel?.text = entryData["title"] as? String | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment