Skip to content

Instantly share code, notes, and snippets.

@Starefossen
Last active January 16, 2023 09:50
Show Gist options
  • Save Starefossen/689428b6c532d7fec0bb to your computer and use it in GitHub Desktop.
Save Starefossen/689428b6c532d7fec0bb to your computer and use it in GitHub Desktop.
Remote JSON Data to tableView in iOS 8.0 (Swift)
import UIKit
import Foundation
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()
class RemoteAPI {
func getData(completionHandler: ((NSArray!, NSError!) -> Void)!) -> Void {
let url: NSURL = NSURL(string: "http://itunes.apple.com/search?term=Turistforeningen&media=software")
let ses = NSURLSession.sharedSession()
let task = ses.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
if (error != nil) {
return completionHandler(nil, error)
}
var error: NSError?
let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
if (error != nil) {
return completionHandler(nil, error)
} else {
return completionHandler(json["results"] as [NSDictionary], nil)
}
})
task.resume()
}
}
var api = RemoteAPI()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
var items: NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
self.tableView = UITableView(frame:self.view!.frame)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view?.addSubview(self.tableView)
api.getData({data, error -> Void in
if (data != nil) {
self.items = NSMutableArray(array: data)
self.tableView!.reloadData()
self.view
} else {
println("api.getData failed")
println(error)
}
})
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
if let navn = self.items[indexPath.row]["trackName"] as? NSString {
cell.textLabel.text = navn
} else {
cell.textLabel.text = "No Name"
}
if let desc = self.items[indexPath.row]["description"] as? NSString {
cell.detailTextLabel.text = desc
}
return cell
}
}
ViewController().view
@Proadik
Copy link

Proadik commented Feb 14, 2015

OMG! THANK YOU SO MUCH!!!!

@mvannakpanha
Copy link

OMG, that's great!

@zhmz1326
Copy link

It's Great! Thank you.

@makohn
Copy link

makohn commented Sep 20, 2015

Thanks, that really helped me a lot. ;)

@vai21
Copy link

vai21 commented Nov 2, 2015

OMG

@telatin
Copy link

telatin commented Jan 14, 2016

Hi!
This looks really cool, but importing the project in XCode makes it proposing to convert to newer Swift and at the end many errors will prevent it from building...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment