I want to change the image in a custom UITableViewCell
, depending on whether it is selected or not. So, when user selects a row, the image in two rows has to be changed (both the current selected and the one to be selected). Calling reloadData
, or reloadRowsAtIndexPaths
from didSelectRowAtIndexPath
doesn't work because it deselects the current selected row, which I don't want to happen. Calling reloadData
from willDeselectRowAtIndexPath
and willSelectRowAtIndexPath
also causes the same problem. Anyone know a solution for this?
Did you try just programmatically selecting the row in cellForRowAtIndexPath
based on a stored property indicating the selected row?
I would an approach where you still use reloadData
, or reloadRowsAtIndexPaths
from didSelectRowAtIndexPath
but first save the selected row in a property so that you can manually set it as selected when the cell is redrawn.
For example:
var selectedRow:NSIndexPath? = nil
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRow = indexPath
tableView.reloadData() // or reload a specific row
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCellReuseIdentifier")
if (indexPath == selectedRow) {
cell!.selected = true
} else {
cell!.selected = false
}
}
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.