Ich habe eine UIButton in einer UITableViewCell, wo sich das Bild ändert, sobald die Schaltfläche angetippt wird. Obwohl die ausgewählten Schaltflächen wie beabsichtigt ausgewählt wurden, verschwinden die ausgewählten Bilder nach dem erneuten Bildlauf der UITableView, da die Zellen wiederverwendet werden.UITableViewCell Bilder verschwinden beim Scrollen
Ich habe Probleme beim Schreiben der Logik. Bitte helfen Sie.
Mein Code ist unten, in Swift 3.
CellForRow:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//Button_Action
addSongButtonIdentifier(cell: cell, indexPath.row)
}
Dies ist, wo die Zelle erstellt wird:
func addSongButtonIdentifier(cell: UITableViewCell, _ index: Int) {
let addButton = cell.viewWithTag(TABLE_CELL_TAGS.addButton) as! UIButton
//accessibilityIdentifier is used to identify a particular element which takes an input parameter of a string
//assigning the indexpath button
addButton.accessibilityIdentifier = String (index)
// print("visible Index:",index)
print("Index when scrolling :",addButton.accessibilityIdentifier!)
addButton.setImage(UIImage(named: "correct"), for: UIControlState.selected)
addButton.setImage(UIImage(named: "add_btn"), for: UIControlState.normal)
addButton.isSelected = false
addButton.addTarget(self, action: #selector(AddToPlaylistViewController.tapFunction), for:.touchUpInside)
}
Die Tap-Funktion:
func tapFunction(sender: UIButton) {
print("IndexOfRow :",sender.accessibilityIdentifier!)
// if let seporated by a comma defines, if let inside a if let. So if the first fails it wont come to second if let
if let rowIndexString = sender.accessibilityIdentifier, let rowIndex = Int(rowIndexString) {
self.sateOfNewSongArray[rowIndex] = !self.sateOfNewSongArray[rowIndex]//toggle the state when tapped multiple times
}
sender.isSelected = !sender.isSelected //image toggle
print(" Array Data: ", self.sateOfNewSongArray)
selectedSongList.removeAll()
for (index, element) in self.sateOfNewSongArray.enumerated() {
if element{
print("true:", index)
selectedSongList.append(songDetailsArray[index])
print("selectedSongList :",selectedSongList)
}
}
}
Stellen Sie die gleiche Frage nicht mehrmals. https://stackoverflow.com/questions/45627323/selected-button-of-a-uitablecell-get-disappear-when-scrolling/45629235#45629235 – PGDev
Mögliche Duplikate von [Ausgewählte Schaltfläche einer UITableViewCell verschwinden beim Scrollen] (https : //stackoverflow.com/questions/45627323/selected-button-of-a-uitablecell-get-disappear-when-crolling) – PGDev