Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmixforce
9,747 PointsImporting a JSON that contains imageURL & Text.
I would like to load images and text into a swift tableview. I can successfully load text into the tableview from JSON, however, I don't know how to load the images into it.
the JSON link, at http://www.abcdefg.com/1.json
JSON content is like below,
{
"data":{
"children":[
{
"data":{
"author":"Barrack",
"title":"This is Swift"
"imageURL":"https://lh3.googleusercontent.com/-c9bKgaRfC3Q/AAAAAAAAAAI/AAAAAAAAAWU/4SdoBhdqWig/s120-c/photo.jpg"
}
}
]
}
}
and the viewController.swift is like below,
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableData = []
@IBOutlet weak var simpleListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getSimpleJSON("http://www.abcdefg.com/1.json")
}
func getSimpleJSON(whichSimple : String){
let mySession = NSURLSession.sharedSession()
let url: NSURL = NSURL(string: whichSimple)!
let networkTask = mySession.dataTaskWithURL(url, completionHandler : {data, response, error -> Void in
var err: NSError?
var theJSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSMutableDictionary
let results : NSArray = theJSON["data"]!["children"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.simpleListTableView!.reloadData()
})
})
networkTask.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
let simpleEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary
cell.textLabel?.text = simpleEntry["data"]!["title"] as String
cell.detailTextLabel?.text = simpleEntry["data"]!["author"] as String
return cell
}
}
2 Answers
Daniel Sattler
4,867 PointsYour JSON File from the link youยดve posted is currently empty.
Besides you are missing a comma after "title": "This is Swift"
Patrick Cooney
12,216 PointsPatrick Cooney
12,216 PointsFor the future if you add the name of the language after your 3 tick marks it will add syntax highlighting which makes the code more legible than the all gray on darker gray.