我已经成功地填充了一个UICollectionView控制器与来自CloudKit记录的数据和图像,但是我有问题将选定的单元格传递给细节UIViewController。这里是我的代码迄今 -UICollectionView控制器和传递CloudKit数据准备Segue
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
}
}
}
在该行出现的问题 -
让staffLeader:CKRecord = staffArray [indexPath.row]
我在哪里带有错误 -
类型'(UICollectionViewCell) - > IndexPath?'的值没有成员 “行”
我试图与电池更换行,然而,这只是提出了另一个错误 -
类型的值“(UICollectionViewCell) - > IndexPath”没有会员 'cell'
我确定有一些基本的东西我失踪但看不到它。任何指针不胜感激。
集合视图没有'indexPath'属性。你可能意味着'collectionView?.indexPathsForSelectedItems?.first'? – dan
感谢丹,但是,这改变了我的问题一点,现在我已经收集了第一条记录在选定的indexPath,然后我如何使用它来使下一行工作 'let staffLeader:CKRecord = staffArray [indexPath]' ie从CKRecord收集该项目的所有数据,然后在segue中传递recordID? – Bowcaps
再往前走一点,现在 - 我已经改变了prepareForSegue FUNC按键 - '能战胜FUNC准备(对于SEGUE:UIStoryboardSegue,发件人:任何?){ 如果segue.identifier == “showStaffDetail”{ 让destinationController = SEGUE。目的地为! StaffDetailsVC 如果让indexPath =的CollectionView .indexPathsForSelectedItems。第一?{ 让工作人员:CKRecord = staffArray [indexPath.item] 让STAFFID是= staff.recordID.recordName destinationController.staffID = STAFFID是 }} } ' – Bowcaps