0

I'm fairly new in swift programming. I cannot seem to figure to how to pull thumbnail images and show them on my application.

I have spent countless hours, I have found nothing reliable. I am positive the issue is in my UITableView, but I just don't know how to code it right. Any and all advice/help will be greatly appreciated:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableData = []
@IBOutlet weak var redditListTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    getRedditJSON("http://www.reddit.com/.json")
}

func getRedditJSON(whichReddit : String){
    let mySession = NSURLSession.sharedSession()
    let url: NSURL = NSURL(string: whichReddit)
    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.redditListTableView!.reloadData()
        })
    })
    networkTask.resume()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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 redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary
    cell.textLabel.text = redditEntry["data"]!["title"] as String
    cell.detailTextLabel.text = redditEntry["data"]!["author"] as String
    return cell
}
}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Mike113
  • 33
  • 4
  • Specifically, what is actually happening and how does this conflict with your expected results? – nhgrif Mar 29 '15 at 16:46
  • right now the code is outputting title and subtitle, I would like to pull thumbnail from http://reddit.com/.json and display it in tableview, so I can get something like this [link](http://i.stack.imgur.com/aIBek.png) – Mike113 Mar 29 '15 at 16:50
  • Please include [what you have tried](http://stackoverflow.com/help/how-to-ask) in your code. In your code there is no code that will display any image at all (or download) – milo526 Mar 29 '15 at 17:14
  • This is my output- http://postimg.org/image/63mgiz5ct/ – Mike113 Mar 29 '15 at 17:16

1 Answers1

0

I am not sure I get the question right, but it doesn't seem you are even trying to download the image. Check the JSON from the URL provided ("http://www.reddit.com/.json"), especially the "thumbnail" tag in the json. Copy paste it into your browser and you'll see the image.

The only thing you have to do is load the image(check Loading/Downloading image from URL on Swift for how you do that in SWIFT) and bind it to the imageView of your tableViewCell.

Community
  • 1
  • 1
3vangelos
  • 501
  • 8
  • 15
  • I want to add to this comment that you would want to make a custom cell for this. You should be able to combine the link supplied by 3vangelos with [this tutorial](https://www.youtube.com/watch?v=adP2dG_C1XU) and get the desired output – milo526 Mar 29 '15 at 17:19