Ios 数据源赢得';t在cellForItemAt函数中更新
从我的Ios 数据源赢得';t在cellForItemAt函数中更新,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,从我的cellForItemAt获取更新的数据源时遇到问题 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: i
cellForItemAt
获取更新的数据源时遇到问题
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! QuestLogCollectionViewCell
cell.delegate = self
let task = board[indexPath.section].tasks[indexPath.row]
cell.task = task
cell.taskLabel.text = task.action
cell.ifTaskCompleted = task.ifTaskComplete
return cell
}
当用户点击复选框按钮时,buttonattaped
函数将被调用,并通过协议传递数据
func buttonTapped() {
guard let taskStatus = ifTaskCompleted else {return}
if taskStatus == true {
checkBoxButton.setImage(#imageLiteral(resourceName: "box"), for: .normal)
} else {
checkBoxButton.setImage(#imageLiteral(resourceName: "checkedBox"), for: .normal)
}
delegate?.userMarkedTaskCompleted(ifTaskComplete: !taskStatus, for: self)
}
func userMarkedTaskCompleted(ifTaskComplete: Bool, for cell: QuestLogCollectionViewCell) {
guard let indexPath = self.collectionView?.indexPath(for: cell) else {return}
var tasks = board[indexPath.section].tasks
tasks[indexPath.row].ifTaskComplete = ifTaskComplete
collectionView?.reloadItems(at: [indexPath])
}
行
var tasks=board[indexPath.section]。任务可能有问题。具体来说,如果任务类型是值类型(例如struct
types),则可以更新原始结构的副本
我建议您直接更新板
/任务
结构:
func userMarkedTaskCompleted(ifTaskComplete: Bool, for cell: QuestLogCollectionViewCell) {
guard let indexPath = self.collectionView?.indexPath(for: cell) else {return}
board[indexPath.section].tasks[indexPath.row].ifTaskComplete = ifTaskComplete
collectionView?.reloadItems(at: [indexPath])
}
“我从cellForItemAt获取更新数据源时遇到问题”是什么意思?发生了什么?您是否添加了断点和/或日志语句以了解发生了什么?您进行了哪些调试?是的,我使用了日志语句,并且能够识别cellForItemAt函数中的ifTaskComplete没有反映在更新的ifTaskComplete上。我使用log语句在userMarkedTaskCompleted和cellForItemAt中打印ifTaskComplete。它们不匹配任务是值类型(struct
)还是引用类型(class
)?