6

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) } } 
3
  • 4
    as! cell - this part is problematic, you need a type name to cast to. cell is a variable name. Commented Feb 25, 2018 at 10:47
  • @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? Commented Feb 25, 2018 at 11:27
  • have you made a custom class for the tableViewCell? Commented Feb 25, 2018 at 11:29

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers for the quick response! I have already done that, the error is with the as! cell part
cast as! yourViewController class name . Like - as! collectionViewCell(your storyBoard class name) @ConnorBerry
2
  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.

Comments

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 } 

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

Comments

0

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.