2016-04-23 8 views
1

ausgewählt Ich versuche zu erkennen, ob eine UITableViewCell ausgewählt ist oder nicht in cellForRowAtIndexPath. Das ist, was ich bisher mit Objective C getan habe:Detecting Zelle ist in UITableView in CellForRowAtIndexPath in Swift

UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath]; 
if(cell.isSelected) { 
    NSlog(@"This cell is selected") 
} 

Ich bin auf die Eigenschaft zuzugreifen isSelected der UITableviewCell in Swift nicht in der Lage. Wie erreiche ich das?

Antwort

4

if cell.selected { ist der richtige Weg zu gehen.

if let cell = tableView.cellForRowAtIndexPath(someIndexPath) { 
    if cell.selected { 
    print("This cell is selected") 
    } 
} 

Update: Swift 3

if let cell = tableView.cellForRow(at: someIndexPath) { 
    if cell.isSelected { 
    print("This cell is selected") 
    } 
} 
0

Sie benötigen didSelectRowAtIndexPath Delegatmethode implementieren:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
    print("cell selected at row \(indexPath.row)") 
} 
0

Sie können wie auf diese Weise tun, Holen Sie sich das indexPath

var someIndexPath: NSIndexPath = tableView.indexPathForSelectedRow() 

aufheben, wenn sie tatsächlich mit isSelected Eigenschaft der Zelle ausgewählt ist.

var cell: UITableViewCell = tableView.cellForRowAtIndexPath(someIndexPath) 
if cell.isSelected { 
    tableView.deselectRowAtIndexPath(someIndexPath, animated: true) 
} 
Verwandte Themen