UICollectionView 控制器和在准备 Segue 中传递 CloudKit 数据

Posted

技术标签:

【中文标题】UICollectionView 控制器和在准备 Segue 中传递 CloudKit 数据【英文标题】:UICollectionView Controller and Passing CloudKit data in Prepare for Segue 【发布时间】:2017-03-15 16:08:35 【问题描述】:

我已经成功地使用 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?' 类型的值没有会员 '细胞'

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

【问题讨论】:

集合视图没有indexPath 属性。你的意思可能是collectionView?.indexPathsForSelectedItems?.first 谢谢 dan,但这稍微改变了我的问题,现在我已经收集了所选 indexPath 的第一条记录,然后我如何使用它来使下一行工作let staffLeader: CKRecord = staffArray[indexPath] 即收集所有该项目的 CKRecord 中的数据,然后在 segue 中传递 recordID? 现在更进一步 - 我已将 prepareForSegue 函数更改为 - ` override func prepare(for segue: UIStoryboardSegue, sender: Any?) if segue.identifier == "showStaffDetail" let destinationController = segue.destination 为! StaffDetailsVC if let indexPath = collectionView?.indexPathsForSelectedItems?.first let staff: CKRecord = staffArray[indexPath.item] let staffID = staff.recordID.recordName destinationController.staffID = staffID ` 现在收到以下错误 - Could not cast value of type 'UIViewController' (0x1089b0758) to 'MedTRiM.StaffDetailsVC' (0x104820678) 选择单元格时 【参考方案1】:

如果你的 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
        
    

请注意,我已将方法签名改回标准方法签名。

【讨论】:

这行似乎有问题 - let destinationController = segue.destination as! StaffDetailsVC 出现以下错误 - Could not cast value of type 'UIViewController' (0x110b08758) to 'StaffDetailsVC' (0x10cb1d678) ? @Bowcaps:我从 your 代码中获取了StaffDetailsVC。这应该是目标视图控制器的类的名称 - 将其替换为正确的名称,并确保在 Storyboard 中为该视图控制器设置了正确的类。 这是我的困惑,StaffDetailsVC 是目标 UIViewController,关于正确的类,它应该是 UIViewController(它目前是)还是其他什么?感谢您一直以来的建议。 绝对应该是UIViewController的子类。错误消息意味着您的情节提要中有问题(可能)。请参阅此答案以了解可能解决此新问题:***.com/q/31440964/558933 我发现,像你一样,它可能与 SB 相关,我从情节提要中删除了 VC,重新创建它,连接它,它第一次工作。我不确定实际的故障是什么,但我昨天在 Xcode 中确实发生了崩溃,所以这可能是一个促成因素?再次感谢您对此提供的帮助。

以上是关于UICollectionView 控制器和在准备 Segue 中传递 CloudKit 数据的主要内容,如果未能解决你的问题,请参考以下文章