Connor Berry
Connor Berry

Reputation: 167

Swift Error - Use of undeclared type 'cell' - Collection View

Quite new to programming in Swift, what is the reason to my error and how can I fix it? Many thanks.

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)
    }
}

Upvotes: 6

Views: 4898

Answers (5)

Menaim
Menaim

Reputation: 1014

1- You have to make sure that the cell is connected to the same class you are using.

2- Make sure that you declared the cellIdentifier for the cell.

3- If you created this cell with .xib file then you need to declare it first in the viewDidLoad() by writing the below line:

tableView.register(UINib(nibName: "cell", bundle: nil), forCellReuseIdentifier: "cell")

Upvotes: 0

Shahin
Shahin

Reputation: 885

  1. If you have defined your custom UICollectionViewCell class with class name: MyCell, then use that in this line:
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MyCell
  1. Please make sure that you have chosen the same class name for the cell in your storyboard (By clicking on the cell object and choose your class name in Custom Class field. (see image below)

select custom class

  1. Also make sure that you are using the same Cell Identifier as you have entered in your storyboard.

Upvotes: 2

Ken Mueller
Ken Mueller

Reputation: 4163

Here's a better way of casting the collection view cell to your custom class type, without "force casting" (when you use 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 you can see, I'm using as? instead of as! to cast the cell to type CollectionViewCell.

Upvotes: 0

Harshal Bhavsar
Harshal Bhavsar

Reputation: 1663

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
}

In Your cellForItemAt Method just replace as! cell with Your_Cell_Class_Name As there is no class named cell in your code.

Happy Coding

Upvotes: 0

Rashed
Rashed

Reputation: 2425

You have to set an Identifier in your Attributes inspector.

  1. click on the entire cell in your storyboard.

  2. Select Attributes inspector

  3. Provide a identifier name (e.g. identifier= cell)

    For more clarification- First Image
    Second Image

Upvotes: 3

Related Questions