我已经成功地使用 CloudKit 记录中的数据和图像填充了 UICollectionView Controller ,但是我在将所选单元格传递给详细信息 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(contentsOficonImage?.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]
我遇到错误的地方 -
Value of type '(UICollectionViewCell) -> IndexPath?' has no member
'row'
我尝试用单元格替换行,但这只会出现另一个错误 -
Value of type '(UICollectionViewCell) -> IndexPath?' has no member
'cell'
我确信我缺少一些基本的东西,但看不到它。任何指针都非常感谢。
Best Answer-推荐答案 strong>
如果你的 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
}
}
}
请注意,我已将方法签名改回标准方法签名。
关于ios - UICollectionView Controller 和在准备 Segue 中传递 CloudKit 数据,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/42815072/
|