-3

Below is my sample JSON data from API. I would like to display "ArticleTitle" and "ImageURL" in tableviewcell using swift..

"ArticleTitles":[{"ArticleID":"872",
 "ArticleTitle":"IS 'NOTHlNGNESS' AN EXPERIENCE ?",  
 "Author":"Asampoorna",
 "ImageURL":"http://motherofall.org/sites/default/files/blog/Ignorance.png"
},{..........}]
Rami
  • 7,879
  • 12
  • 36
  • 66

1 Answers1

0

In the response you are getting the results array. Then after you are trying to o something with the array there itself, which is wrong. After getting results array, all processing should be done in cellForRowAtIndexPath method.

Take the results array as global instead of taking tableData and imageData.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
    let arr:NSArray! = NSArray()
    let dict = arr[indexPath.row] as! NSDictionary
    cell.textLabel?.text = dict["ArticleTitle"] as? String

    //Load the image here with image URL string dict["ImageURL"] as? String

    return cell

}

And follow this post to load image asynchronously in tableViewCell.

Load the image to the image view inside tableview cell using following code.

if let url = NSURL(string: "http://motherofall.org/sites/default/files/blog/Ignorance.png") {
    if let data = NSData(contentsOfURL: url){
        cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit
        cell.imageView.image = UIImage(data: data)
    }
}
Dev
  • 1,215
  • 9
  • 17
  • Hello Dev, We can able to bind single URL or single image to UI . But our problem is we could not bind array of JSON URL's (consuming from our API end point ) to UI . Above is the structure of our JSON array . – Madhuri Karumanchi Aug 26 '15 at 13:59
  • I think you are getting array of dictionaries. Loop that array and get the URL of the image. Then load the image. – Dev Aug 26 '15 at 14:08
  • Sorry.. No need to loop. As you are filling the image in table view cell, in cellForRowAtIndexpath method take the dictionary from array using "indexPath.row" as index. Then u can take the image URL and above code works. – Dev Aug 26 '15 at 14:22
  • Hello dev, thanks for replying. I am newbie to swift programming. I dont understand where I am doing mistake. Please have a look at my code https://drive.google.com/file/d/0B8jxqQxSyqksY1N1OXZzX2hpQlU/view?usp=sharing – Madhuri Karumanchi Aug 27 '15 at 05:59
  • Please don't mind. Can you give me an end to end solution for my problem. Can you provide one sample app to display country(name) and flag(image) in UITableView using this API http://www.androidbegin.com/tutorial/jsonparsetutorial.txt – Madhuri Karumanchi Aug 28 '15 at 06:42