我已成功使用CloudKit记录中的数据和图像填充UICollectionView控制器,但是我在将所选单元格传递给详细信息UIViewController时遇到问题。这是我到目前为止的代码 -
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?没有会员 '细胞'
我确信有一些基本的东西我不见了但却看不到它。任何指针都非常感激。
答案 0 :(得分:2)
如果你的segue是通过触摸一个单元格触发的,那么你需要以下代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showStaffDetail" {
let destinationController = segue.destination as! StaffDetailsVC
// Find the correct indexPath for the cell that triggered the segue
// And check that the sender is, in fact, a StaffCVCell
if let indexPath = collectionView?.indexPath(for: sender), let sender = sender as? StaffCVCell {
// Get your CKRecord information
let staffLeader: CKRecord = staffArray[indexPath.item]
let staffID = staffLeader.recordID.recordName
// Set any properties needed on your destination view controller
destinationController.staffID = staffID
}
}
}
请注意,我已将方法签名更改回标准方法签名。