2015-02-06 5 views
7

Guten Morgenmeine Tabellenansicht die ausgewählten Zellen wiederverwendet werden, wenn Scroll - in SWIFT

mein Problem ist, dass meine Tabellenansicht die ausgewählte Zelle wieder verwenden, wenn ich nach unten scrollen und wieder .. ich meine, wenn ich eine Zelle wählen Sie aus Dann scrolle ich nach unten, einige Zellen, die ich nicht ausgewählt habe, sind ausgewählt, auch einige ausgewählte Zellen von oben werden nicht ausgewählt, wenn ich wieder hoch scrolle, und wenn das passiert, bin ich wieder Apfel, um mehr als nur eine Zelle auszuwählen, die nicht sein muss erlaubt .. ich erwähnen möchte, dass ich versuchte, den alten Index-Pfad zu retten ‚didSelectRowAtIndexPath'and i es so in das aufgegebene‘ CellForRowAtIndexPath'but es nicht funktioniert:

if (old == indexpath) 
{ 
     cell?.backgroundColor = UIColor.redColor() 
     cell?.textLabel?.backgroundColor = UIColor.whiteColor() //to change only the thumbnail color and keep the cell color 
} 

else { 
     cell?.backgroundColor = UIColor.whiteColor() 
} 

hier nun mein Code

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = myTable.dequeueReusableCellWithIdentifier("WorkCell") as UITableViewCell 
    cell.tag = (indexPath.row) + 1 
    cell.textLabel?.text = Berufs[indexPath.row] 
    var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40)) 
    cell.selectionStyle = UITableViewCellSelectionStyle.None 
    img.image = UIImage(named: "transparent.png") 
    cell.indentationLevel = 1 
    cell.indentationWidth = 45 
    cell.addSubview(img) 
    return cell 
} 

var old = 1000 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    let indexPath = tableView.indexPathForSelectedRow() 
    let cell = tableView.cellForRowAtIndexPath(indexPath!) 
    var tmpCell = view.viewWithTag(old) 
    var rowNum = (cell?.tag)! - 1 

    if (cell?.backgroundColor == UIColor.redColor()) 
    { 
     cell?.backgroundColor = UIColor.whiteColor() 
    } 
    else if (cell?.backgroundColor != UIColor.redColor()) 
    { 
     tmpCell?.backgroundColor = UIColor.whiteColor() 
     cell?.backgroundColor = UIColor.redColor() 
     cell?.textLabel?.backgroundColor = UIColor.whiteColor() 

     println("du interssiert dich für \(Berufs[rowNum])") 
    } 

    old = rowNum + 1 

} 

Antwort

14

Sie zur Zeit speichern den Auswahlstatus in der background~~POS=TRUNC . Das ist keine gute Idee, weil die Zellen aus Performance-Gründen von der tableView wiederverwendet werden.

Sie sollten die ausgewählten IndexPaths im Datenmodell speichern. Wenn Sie die Mehrfachauswahl zulassen möchten, können Sie die ausgewählten indexPaths in einem NSMutableSet speichern.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
    cell.selectionStyle = .None 
    configure(cell, forRowAtIndexPath: indexPath) 
    return cell 
} 

var selectedIndexPaths = NSMutableSet() 

func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.textLabel!.text = "Row \(indexPath.row)" 
    if selectedIndexPaths.containsObject(indexPath) { 
     // selected 
     cell.backgroundColor = UIColor.redColor() 
    } 
    else { 
     // not selected 
     cell.backgroundColor = UIColor.whiteColor() 
    } 
} 


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if selectedIndexPaths.containsObject(indexPath) { 
     // deselect 
     selectedIndexPaths.removeObject(indexPath) 
    } 
    else { 
     // select 
     selectedIndexPaths.addObject(indexPath) 
    } 
    let cell = tableView.cellForRowAtIndexPath(indexPath)! 
    configure(cell, forRowAtIndexPath: indexPath) 
} 

Wenn Sie nur einzelne Auswahl sollten Sie die ausgewählte indexPath in einem NSIndexPath retten? Instanzvariable

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
    cell.selectionStyle = .None 
    configure(cell, forRowAtIndexPath: indexPath) 
    return cell 
} 

var selectedIndexPath: NSIndexPath? 

func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.textLabel!.text = "Row \(indexPath.row)" 
    if selectedIndexPath == indexPath { 
     // selected 
     cell.backgroundColor = UIColor.redColor() 
    } 
    else { 
     // not selected 
     cell.backgroundColor = UIColor.whiteColor() 
    } 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if selectedIndexPath == indexPath { 
     // selected same cell -> deselect all 
     selectedIndexPath = nil 
    } 
    else { 
     // select different cell 
     let oldSelectedIndexPath = selectedIndexPath 
     selectedIndexPath = indexPath 

     // refresh old cell to clear old selection indicators 
     var previousSelectedCell: UITableViewCell? 
     if let previousSelectedIndexPath = oldSelectedIndexPath { 
      if let previousSelectedCell = tableView.cellForRowAtIndexPath(previousSelectedIndexPath) { 
       configure(previousSelectedCell, forRowAtIndexPath: previousSelectedIndexPath) 
      } 
     } 
    } 
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! 
    configure(selectedCell, forRowAtIndexPath: indexPath) 
} 
+0

funktioniert wie ein Charme * __ * thankssssssss –

+0

Diese Antwort ist großartig und funktioniert wie ein Charme, aber warum haben Sie vor einigen der Funktionen überschrieben? Solange der Controller UITableViewDelegate implementiert, sollte man nur die Funktion aber kein "Override" haben? –

+0

danke U habe mir auch meine Zeit gerettet :) – user1553381

-1

I ist vor dem gleichen Problem eine Weile hatte, was ich tat, ist, habe ich eine neue Variable (Bool) genannt isset zum CustomTableViewCell, und als ich geschaffen, um die Zelle für die ersten Mal habe ich es auf wahr, so, nachdem ich eine bereits gesetzt Zelle, ich sind gerade die arleady eingestellten, nicht gesetzt ist wieder verwenden wollte:

let cell = tableView.dequeueReusableCellWithIdentifier("Cellidentifier", forIndexPath: indexPath) as CustomTableViewCell 

if cell.isset 
{ 
     return cell 
} 

//set your cell here 
cell.isset = true 
return cell 
+0

Ich habe diese Funktion 'isett' nicht gefunden, hast du sie selbst erstellt oder ist sie eingebaut? –

+0

Haben Sie versucht, den Code auszuführen, oder erhalten Sie einen Fehler? Manchmal zeigt XCode nicht alle Methoden an. Und Sie haben diesen Code in CellForRowAtIndexPath implementiert, richtig? –

+0

es zeigte einen Fehler .. ja .. ich habe es dort –

Verwandte Themen