2017-03-15 30 views
2

我已经成功地填充了一个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'

我确定有一些基本的东西我失踪但看不到它。任何指针不胜感激。

+0

集合视图没有'indexPath'属性。你可能意味着'collectionView?.indexPathsForSelectedItems?.first'? – dan

+0

感谢丹,但是,这改变了我的问题一点,现在我已经收集了第一条记录在选定的indexPath,然后我如何使用它来使下一行工作 'let staffLeader:CKRecord = staffArray [indexPath]' ie从CKRecord收集该项目的所有数据,然后在segue中传递recordID? – Bowcaps

+0

再往前走一点,现在 - 我已经改变了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

回答

2

如果您赛格瑞通过触摸一个细胞触发的,你想沿着下面的代码行的东西:

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 
     } 
    } 
} 

请注意,我已经改变了方法签名回标准方法签名。

+0

似乎与此行的一个问题 - '让destinationController = segue.destination as! StaffDetailsVC'发生以下错误 - '无法将类型'UIViewController'(0x110b08758)的值转换为'StaffDetailsVC'(0x10cb1d678)'? – Bowcaps

+0

@Bowcaps:我从*你的*代码中提取了'StaffDetailsVC'。这应该是目标视图控制器类的名称 - 用正确的名称替换它,并确保为故事板中的该视图控制器设置了正确的类。 –

+0

这是我的困惑,StaffDetailsVC是目标UIViewController,关于正确的类,它应该是一个UIViewController(它目前是)还是别的?感谢您持续的建议。 – Bowcaps