【问题标题】:How to display Different Images for different Index in TableView?如何在 TableView 中为不同的索引显示不同的图像?
【发布时间】:2014-12-10 07:59:18
【问题描述】:

我正在开发一个简单的表格应用程序,我想在每个单元格中显示图像,我已经这样做了,一个图像显示在所有单元格中:

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
}

它工作正常,但我想为不同的单元格显示更多图像,并且我已经采用了数组:

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"]

但现在我不知道如何在 tableView 中为不同的索引显示不同的图像。

我知道在 Objective-C 中我们可以这样做:

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

但我不知道如何快速做到这一点。

请帮帮我。

【问题讨论】:

    标签: ios image uitableview swift


    【解决方案1】:
    var imagesArray = [UIImage]()  
    imagesArray.append(UIImage(named: "mypic.png")!)  
    cell.imageView?.image = imagesArray[indexPath.row]  
    return cell
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    • 基本上,您将图像添加到数组中,并根据索引(位置)将图像分配给每个单元格。如果您有一个包含项目“a,b,c”的静态数组,并且您知道“项目 a”应该有什么图像,那么在您的图像数组中您应该首先添加该图像,因为“项目 a”在数组中是第一个(位置零)
    【解决方案2】:

    使用下面的代码

     cell.imageView?.image = UIImage(self.iteams(indexPath.row)
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 我需要 8 分钟。:)
      猜你喜欢
      • 1970-01-01
      • 2014-02-27
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多