2017-02-03 4 views
2

Anforderung;Swift tableView.reloadRows Löschen von Zellen

Ich habe eine statische UITableView und ich versuche, eine pickerview zu öffnen, wenn eine der Zellen angeklickt wird. Ich ändere die Größe mit heightForRowAt so, wenn der cell angeklickt wird, muss ich reload es so, dass die Größe ändert. Wenn ich reloadData() verwenden es funktioniert perfekt (Ich möchte reloadRows verwenden, so kann ich Animation hinzufügen)

Ausgabe:

wenn ich reloadRows(at..., with: .automatic) die Zeilen versuchen einfach verschwinden. Wenn ich reloadRows in viewWillAppear nichts ändert, verschwindet die Zeile nicht.

Code:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(indexPath) 
    if colorCelllExpandedIndex != nil{ 
     let prev = colorCelllExpandedIndex 
     colorCelllExpandedIndex = nil 
     tableView.reloadRows(at: [prev!], with: .automatic) 
     //tableView.reloadData() 
    } else { 
     colorCelllExpandedIndex = indexPath 
     colorCell.frame.size.height = CGFloat(colorCellExpandedHeight) 
     tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 

} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    if indexPath == colorCelllExpandedIndex { 
     return CGFloat(colorCellExpandedHeight) 
    } else { 
     if indexPath.row == 1 { 
      return CGFloat(colorCellRegularHeight) 
     } 
     return UITableViewAutomaticDimension 
    } 
} 

Bildschirmbilder:

before and after clicking the color button

How it looks like if I use reloadData

Antwort

2

Es scheint, dass einige Probleme mit Funktionen wie reloadRows(at:with:) da sind, wenn Mit einer statischen UITableView gibt es eine ähnliche Frage in dieser SO questionohne Auflösung.

konnte ich jedoch die Zellenhöhe zu aktualisieren, indem Sie die folgenden Funktionen Implementierung:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(indexPath) 
    if colorCelllExpandedIndex != nil{ 
     colorCelllExpandedIndex = nil 
    } else { 
     colorCelllExpandedIndex = indexPath 
    } 
    tableView.beginUpdates() 
    tableView.endUpdates() 

} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath == colorCelllExpandedIndex { 
     return CGFloat(colorCellExpandedHeight) 
    } else { 
     if indexPath.row == 0 { 
      return CGFloat(colorCellRegularHeight) 
     } 
     return UITableViewAutomaticDimension 
    } 
} 

in inspiriert Dies wird:

tableView.beginUpdates() 
tableView.endUpdates() 

So ein Ansatz Sie folgende nehmen könnte Eine der Antworten in another SO question zum Ausblenden von Zellen in statischen Tabellenansichten.

Verwandte Themen