gpt4 book ai didi

ios - dataSource 不会在 cellForItemAt 函数中更新

转载 作者:行者123 更新时间:2023-11-28 06:12:45 26 4
gpt4 key购买 nike

我在从我的 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
}

当用户点击复选框按钮时,buttonTapped 函数将被调用并通过协议(protocol)传递数据。

    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].tasks 可能有问题。具体来说,如果您的任务类型是值类型(例如 struct 类型),那么您可以更新原始结构的副本。

我建议你直接更新board/tasks结构:

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])
}

关于ios - dataSource 不会在 cellForItemAt 函数中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46064396/

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