2017-08-25 2 views
0

Hier ist das Problem, wenn ich einen TableViewController verwende und ein Verhalten auf Zelle hinzugefügt habe. Das Verhalten zeigte zweimalSo vermeiden Sie das Kopierverhalten von wiederverwendbaren Zellen

Wie kann ich das vermeiden?

// MARK: - Table Deleget 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 

    UIView.animate(withDuration: 0.2, animations: { 
     cell?.viewWithTag(100)?.isHidden = true 
     (cell?.viewWithTag(200) as! UILabel).textColor = UIColor.red 
    }) 

} 

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 

    UIView.animate(withDuration: 0.3, animations: { 
     cell?.viewWithTag(100)?.isHidden = false 
     (cell?.viewWithTag(200) as! UILabel).textColor = UIColor(red: 0, green: 128/255, blue: 128/255, alpha: 1) 
    }) 

} 

first one identical one

+0

Bitte geben Sie die Frage klären. Fragen, die nach Debugging-Hilfe fragen, müssen den Code enthalten, der das Problem verursacht, das erwartete Verhalten und das tatsächliche Verhalten. –

+0

Verwenden Sie bitte, lassen Sie Methode in Ihrem Code, dann überprüfen. – Mehul

+0

@Mehul Danke, App ist jetzt nicht Absturz, aber zeigt immer noch das gleiche Verhalten –

Antwort

1

Bewegen Sie die Animation aus dem 'cellForRow' Methode 'willDisplayCell' Methode. Ich denke, es kann dir helfen, die doppelte Animation zu vermeiden.

1

Ich habe es behoben, fügen Sie eine var, um die Zelle zu erinnern, die aufgenommen wurde, und verwenden cellWillDisplay, um jede Zelle aktualisiert wird angezeigt, überprüfen Sie jede Zelle, wenn es ausgewählt wurde, wenn, zeigen Sie den ausgewählten Weg.

// MARK: - Table Deleget 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    index = indexPath.row 
    if let cell = tableView.cellForRow(at: indexPath){ 
     UIView.animate(withDuration: 0.2, animations: { 
      cell.viewWithTag(100)?.isHidden = true 
      (cell.viewWithTag(200) as! UILabel).textColor = UIColor.red 
     }) 
    } 
} 

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    if let cell = tableView.cellForRow(at: indexPath) { 
     UIView.animate(withDuration: 0.3, animations: { 
      cell.viewWithTag(100)?.isHidden = false 
      (cell.viewWithTag(200) as! UILabel).textColor = UIColor(red: 0, green: 128/255, blue: 128/255, alpha: 1) 
     }) 
    } 
} 

// Added this 
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if let index = index, index == indexPath.row { 
     cell.viewWithTag(100)?.isHidden = true 
     (cell.viewWithTag(200) as! UILabel).textColor = UIColor.red 
    } else { 
     cell.viewWithTag(100)?.isHidden = false 
     (cell.viewWithTag(200) as! UILabel).textColor = UIColor(red: 0, green: 128/255, blue: 128/255, alpha: 1) 
    } 
} 

I have fixed it

Verwandte Themen