5
votes

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)
    }
}
5
as! cell - this part is problematic, you need a type name to cast to. cell is a variable name.Cristik
@Cristik - Cheers for the quick response. Sorry as I'm new to swift not sure what you mean - could you provide some detail to what I can do to fix?Connor Berry
have you made a custom class for the tableViewCell?Aryan Sharma

5 Answers

2
votes

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

0
votes
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

0
votes

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.

0
votes
  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.
0
votes

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