uicollectionView控制器和传递CloudKit数据准备SEGUE



我已经成功地填充了一个带有来自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(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
        }
    }
}

问题发生在线 -

让员工领导者:ckrecord = staffArray [indexpath.row]

我出现错误的地方 -

类型的值'(uicollectionViewCell) -> indexpath?''没有成员 '行'

我尝试用单元格替换行,但是这只会出现另一个错误 -

类型的值'(uicollectionViewCell) -> indexpath?''没有成员 'cell'

我敢肯定,我缺少一些基本的东西,但看不到它。任何指针都非常感谢。

如果您的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
        }
    }
}

请注意,我将方法签名更改为标准方法签名。

最新更新