2017-09-05 1 views
1

Ich habe Probleme mit der Aktualisierung der dataSource von meinem cellForItemAt.dataSource wird in der Funktion cellForItemAt nicht aktualisiert.

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 
} 

, wenn der Benutzer die Taste checkBox angezapft wird buttonTapped Funktion aufgerufen und Daten über Protokoll übergeben werden.

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

Was Sie bedeuten Sie durch Was ist los? Haben Sie Haltepunkte und/oder Protokollanweisungen hinzugefügt, um herauszufinden, was passiert? Welches Debugging hast du gemacht? – Rob

+0

Ja, ich habe Log-Anweisungen verwendet und konnte feststellen, dass ifTaskComplete in der Funktion cellForItemAt nicht auf der aktualisierten ifTaskComplete reflektiert. Ich habe log-Anweisung verwendet, um die ifTaskComplete in sowohl userMarkedTaskCompleted und CellForItemAt zu drucken. Sie stimmen nicht überein –

+0

Ist Ihre Aufgabe ein Werttyp ('struct') oder ein Referenztyp (' class')? – Rob

Antwort

1

Die Leitung var tasks = board[indexPath.section].tasks könnte problematisch sein. Insbesondere, wenn Ihre Aufgabentypen Werttypen sind (z. B. struct Typen), könnten Sie eine Kopie der ursprünglichen Struktur aktualisieren.

Ich würde vorschlagen, dass Sie die direkt board/tasks Struktur aktualisieren: „Ich habe Problem aktualisiert Datasource von meinem cellForItemAt bekommen“

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