Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 数据源赢得';t在cellForItemAt函数中更新_Ios_Swift_Uicollectionview - Fatal编程技术网

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
)?