gpt4 book ai didi

ios - UICollectionView Controller 和在 Prepare for Segue 中传递 CloudKit 数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:25:13 25 4
gpt4 key购买 nike

我已经成功地使用 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
}
}
}

问题出现在行-

让 staffLeader: CKRecord = staffArray[indexPath.row]

出现错误的地方-

Value of type '(UICollectionViewCell) -> IndexPath?' has no member 'row'

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

Value of type '(UICollectionViewCell) -> IndexPath?' has no member '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
}
}
}

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

关于ios - UICollectionView Controller 和在 Prepare for Segue 中传递 CloudKit 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com