ios - 'UICollectionViewCell' 没有名为 'image' [Swift] 的成员

标签 ios swift uicollectionview uicollectionviewcell

我正在设置一个带有 UICollectionView 的项目,该项目显示带有标签的图像。我创建了 ViewControllerCellViewController,就像它应该的那样。

CellViewController 中,代码如下:

class CellController: UICollectionViewCell {

    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var label: UILabel!


}

但是在 ViewController 中设置图像时出现错误。这是代码:

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

    let cell: UICollectionViewCell = collection.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellController

    cell.image.image = UIImage(named: "star")

    return cell
}

最奇怪的是,我从 GitHub 下载了一个项目,它与我的基本相同,而且运行良好。

提前致谢!

最佳答案

您的问题是 :UICollectionViewCell。虽然您将单元格转换为 CellController,但这会使类型 UICollectionViewCell 没有图像属性。删除它或将其替换为 CellController 将解决您的问题。

let cell = collection.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellController

我还会考虑将您的 ImageView 重命名为 imageViewcell.image.image 行有些地方看起来不对!

关于ios - 'UICollectionViewCell' 没有名为 'image' [Swift] 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30818816/

相关文章:

iphone - UICollectionViewController 中加载数据的问题

ios - 使用 Alamofire 调用服务器时未调用 UICollectionView cellForItemAtIndexPath

iOS : How to detect Shake motion?

ios - '(字符串)-> AnyObject ?' is not convertible to ' ResultCell'

ios - collectionViewLayout sizeForItemAtIndexPath -> CGSize 奇怪的行为

ios - 如何防止应用程序在 Xcode 中进行 UI 测试后自动退出?

ios - Parse中拉取PFUser.currentUser的指针域数据

ios - CollectionView 窗口保持黑色

ios - XCode 不打开管理器

ios - 无法呈现 UISearchController