菜鸟教程小白 发表于 2022-12-13 00:12:40

ios - UICollectionView Controller 和在准备 Segue 中传递 CloudKit 数据


                                            <p><p>我已经成功地使用 CloudKit 记录中的数据和图像填充了 UICollectionViewController ,但是我在将所选单元格传递给详细信息 UIViewController 时遇到问题。到目前为止,这是我的代码 -</p>

<pre><code>override func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
    return self.staffArray.count

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; StaffCVCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! StaffCVCell

    let staff: CKRecord = staffArray

    let iconImage = staff.object(forKey: &#34;staffIconImage&#34;) as? CKAsset
    let iconData : NSData? = NSData(contentsOf:(iconImage?.fileURL)!)


    let leaderNameCell = staff.value(forKey: &#34;staffName&#34;) 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 == &#34;showStaffDetail&#34; {
      let destinationController = segue.destination as! StaffDetailsVC
      if let indexPath = collectionView?.indexPath {
            let staffLeader: CKRecord = staffArray
            let staffID = staffLeader.recordID.recordName
            destinationController.staffID = staffID
      }
    }
}
</code></pre>

<p>问题出现在线路上-</p>

<p>让 staffLeader: CKRecord = staffArray</p>

<p>我遇到错误的地方 -</p>

<blockquote>
<p>Value of type &#39;(UICollectionViewCell) -&gt; IndexPath?&#39; has no member
&#39;row&#39;</p>
</blockquote>

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

<blockquote>
<p>Value of type &#39;(UICollectionViewCell) -&gt; IndexPath?&#39; has no member
&#39;cell&#39;</p>
</blockquote>

<p>我确信我缺少一些基本的东西,但看不到它。任何指针都非常感谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果你的 segue 是通过触摸一个单元格来触发的,你需要下面的代码行:</p>

<pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == &#34;showStaffDetail&#34; {

      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
            let staffID = staffLeader.recordID.recordName

            // Set any properties needed on your destination view controller
            destinationController.staffID = staffID
      }
    }
}
</code></pre>

<p>请注意,我已将方法签名改回标准方法签名。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionViewController 和在准备 Segue 中传递 CloudKit 数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42815072/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42815072/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView Controller 和在准备 Segue 中传递 CloudKit 数据