I am trying to detect whether a UITableViewCell
is selected or not in cellForRowAtIndexPath
. This is what I have done so far with objective C:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
if(cell.isSelected) {
NSlog(@"This cell is selected")
}
I'm not able to access the property isSelected
of the UITableviewCell
in Swift. How do I achieve this?
if cell.selected {
is the right way to go.
if let cell = tableView.cellForRowAtIndexPath(someIndexPath) {
if cell.selected {
print("This cell is selected")
}
}
if let cell = tableView.cellForRow(at: someIndexPath) {
if cell.isSelected {
print("This cell is selected")
}
}
You need to implement delegate method didSelectRowAtIndexPath
:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
print("cell selected at row \(indexPath.row)")
}
You can do like this way, Get the indexPath
var someIndexPath: NSIndexPath = tableView.indexPathForSelectedRow()
Deselect when it is actually selected with isSelected property of cell.
var cell: UITableViewCell = tableView.cellForRowAtIndexPath(someIndexPath)
if cell.isSelected {
tableView.deselectRowAtIndexPath(someIndexPath, animated: true)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.