Swift Error - 使用未声明的类型' cell' - 收藏视图

时间:2018-02-25 10:40:36

标签: ios swift uicollectionview

Swift编程的新手,我的错误是什么原因以及如何解决?非常感谢。

import UIKit
import Photos


class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate

{

    @IBOutlet weak var myCollectionView: UICollectionView!


    var imagesArray = [UIImage]()

    override func viewDidLoad(){
        super.viewDidLoad()
        myCollectionView.delegate = self
        myCollectionView.dataSource = self

    }

    @IBAction func addPhoto(_ sender: AnyObject) {

        let picker:UIImagePickerController = UIImagePickerController()
        picker.sourceType = .photoLibrary
        picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!

        picker.delegate = self
        picker.allowsEditing = false
        self.present(picker, animated: true, completion: nil)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! cell


        cell.configurecell(imagesArray[indexPath.row])

        return cell

    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return imagesArray.count

    }



    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedimage = (info[UIImagePickerControllerOriginalImage] as? UIImage){
            imagesArray = [pickedimage,pickedimage,pickedimage]//Will store three selected images in your array
            myCollectionView.reloadData()
        }
        dismiss(animated: true, completion: nil)
    }
}

5 个答案:

答案 0 :(得分:1)

您必须在“属性”检查器中设置标识符。

  1. 点击故事板中的整个单元格。
  2. 选择属性检查器
  3. 提供标识符名称(例如标识符=单元格)

    更多澄清 - 第一张图片
    第二张图片

答案 1 :(得分:0)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! Your_Cell_Class_Name
        cell.configurecell(imagesArray[indexPath.row])
        return cell
}

在您的cellForItemAt方法中,只需将as! cell替换为Your_Cell_Class_Name因为代码中没有名为cell的类。

快乐编码

答案 2 :(得分:0)

这是将集合视图单元格转换为自定义类类型的更好方法,而无需“强制转换”(使用as!时):

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let _cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", for: indexPath)
    guard let cell = _cell as? CollectionViewCell else { return _cell } // Replace "CollectionViewCell" with your custom class name
    cell.configurecell(imagesArray[indexPath.row])
    return cell
}

如您所见,我使用as?而不是as!来将单元格强制转换为CollectionViewCell

答案 3 :(得分:0)

1- 您必须确保单元连接到您正在使用的同一类。

2- 确保您为单元声明了 const { admin } = require("firebase-admin");

3- 如果您使用 cellIdentifier 文件创建此单元格,那么您需要先在 .xib 中通过编写以下行来声明它:

viewDidLoad()

答案 4 :(得分:-1)

如果您是以编程方式创建自定义集合ViewCell而不是使用StoryBoards,那么以下步骤将对您有所帮助。

//declare cellId
let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

  //Don't forget to register cell
    collectionView?.register(**YourCustomCellName**.self, forCellWithReuseIdentifier: cellId)
} 

然后在cellForItemAt方法

中投射自定义单元格
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! **YourCustomCellName**
    return cell
}
相关问题