Reputation: 167
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
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
Reputation: 885
UICollectionViewCell
class with class name: MyCell
, then use that in this line:let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MyCell
Custom Class
field. (see image below)Cell Identifier
as you have entered in your storyboard.Upvotes: 2
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
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
Reputation: 2425
You have to set an Identifier in your Attributes inspector.
click on the entire cell in your storyboard.
Select Attributes inspector
Provide a identifier name (e.g. identifier= cell)
For more clarification-
First Image
Second Image
Upvotes: 3