I have successfully populated a UICollectionView controller with data and images from a CloudKit record, however I am having a problem passing the selected cell to the details UIViewController. Here is my code thus far -
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.staffArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> StaffCVCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! StaffCVCell
let staff: CKRecord = staffArray[indexPath.row]
let iconImage = staff.object(forKey: "staffIconImage") as? CKAsset
let iconData : NSData? = NSData(contentsOf:(iconImage?.fileURL)!)
let leaderNameCell = staff.value(forKey: "staffName") as? String
cell.leaderNameLabel?.text = leaderNameCell
cell.leaderImageView?.image = UIImage(data:iconData! as Data);
return cell
}
func prepare(for segue: UIStoryboardSegue, sender: StaffCVCell) {
if segue.identifier == "showStaffDetail" {
let destinationController = segue.destination as! StaffDetailsVC
if let indexPath = collectionView?.indexPath {
let staffLeader: CKRecord = staffArray[indexPath.row]
let staffID = staffLeader.recordID.recordName
destinationController.staffID = staffID
}
}
}
The problem occurs at the line -
let staffLeader: CKRecord = staffArray[indexPath.row]
where I am presented with the error -
Value of type '(UICollectionViewCell) -> IndexPath?' has no member 'row'
I have tried replacing row with cell, however this only presents another error -
Value of type '(UICollectionViewCell) -> IndexPath?' has no member 'cell'
I am sure there is something fundamental i'm missing but cannot see it. Any pointers greatly appreciated.
indexPath
property. You might meancollectionView?.indexPathsForSelectedItems?.first
? – danlet staffLeader: CKRecord = staffArray[indexPath]
i.e. collect all the data from the CKRecord for that item and then pass the recordID in the segue? – BowcapsCould not cast value of type 'UIViewController' (0x1089b0758) to 'MedTRiM.StaffDetailsVC' (0x104820678)
when selecting a cell – Bowcaps