UICollectionView 控制器并在 Prepare for Segue 中传递 CloudKit 数据
UICollectionView Controller and Passing CloudKit data in Prepare for Segue
我已经成功地使用 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]
出现错误的地方 -
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
}
}
}
请注意,我已将方法签名改回标准方法签名。
我已经成功地使用 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]
出现错误的地方 -
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
}
}
}
请注意,我已将方法签名改回标准方法签名。