简体   繁体   中英

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.

您可以使用以下代码从数组中获取图像:

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

使用以下代码

 cell.imageView?.image = UIImage(self.iteams(indexPath.row)
var imagesArray = [UIImage]()  
imagesArray.append(UIImage(named: "mypic.png")!)  
cell.imageView?.image = imagesArray[indexPath.row]  
return cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM