【问题标题】:Can't assign images from an array into a collection view无法将数组中的图像分配到集合视图中
【发布时间】:2015-02-12 01:51:41
【问题描述】:

我正在尝试将数组中的图像资产分配到集合视图中。我正在努力解决一条线。有人对我做错了什么有建议吗?问题出在单元格项目上。我的代码是:

         override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as CollectionViewCell
        println("just seeing when this prints")
        println(self.matchedSelfie)

        cell.match.image = self.matchedSelfie

            return cell

问题在于 cell.match.image = self.matchedSelfie

【问题讨论】:

  • 有什么问题?如果单元格为 nil,则应创建一个新的 CollectionViewCell 实例。 self.matchedSelfie 中有图片吗?
  • matchedSelfie 数组中存储了 9 张图像。我不知道如何将它们分配为匹配项。

标签: arrays image swift uicollectionview


【解决方案1】:

您可以使用以下方法获得正确的图像:

cell.match.image = self.matchedSelfie[indexPath.row]

假设数组中图像的顺序与单元格的顺序相同。

【讨论】:

  • 匹配总是返回为零。我添加了行 ' if cell.match != nil { cell.match.image = self.matchedSelfie[indexPath.row] return cell } else { println("error") return cell' 这只会将错误打印到控制台。
  • 但是你有一个单元格对象吗?因为你只出队一个。如果不能出队,则不要实例化新单元格。如果您确实创建了一个新的单元格实例,您是从 .xib 还是故事板加载它?那么,您是否将图像链接到 .match 属性?
  • ![故事板的屏幕截图] (dropbox.com/s/xcdrbnk13nms5z3/…)
  • 我的单元格代码非常基本:import UIKit class CollectionViewCell: UICollectionViewCell { @IBOutlet var match: UIImageView! }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
相关资源
最近更新 更多