Dharmesh Kheni Dharmesh Kheni - 5 months ago 22
Swift Question

How to display Different Images for different Index in TableView?

I am developing a simple table app where I want to display Image in every cell and I have done this and one Image is displayed In all cells :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

// Create the cell
var cell : UITableViewCell = self.TableViewData.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel?.text = self.iteams[indexPath.row]
cell.imageView?.image = UIImage(named: "creme_brelee.jpg")
return cell

It works fine but I want to display more Images for different cell and for that I have taken on array:

var thumbils : [String] = ["egg_benedict.jpg", "mushroom_risotto.jpg", "full_breakfast.jpg", "hamburger.jpg", "ham_and_egg_sandwich.jpg", "creme_brelee.jpg", "white_chocolate_donut.jpg", "starbucks_coffee.jpg", "vegetable_curry.jpg", "instant_noodle_with_egg.jpg", "noodle_with_bbq_pork.jpg", "japanese_noodle_with_pork.jpg", "green_tea.jpg", "thai_shrimp_cake.jpg", "angry_birds_cake.jpg", "ham_and_cheese_panini.jpg"]

but now I dont know how to display different image for different index in tableView.

I know In objective-C we can do like this:

cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];

But I don't know how to do this in swift.

Please help me for this.


You can use below code to fetch an image from array:

   cell.imageView?.image = UIImage(named: thumbils[indexPath.row])