didDeselectRowAtIndexPath method not triggered after programatically selecting cell in cellForRowAtIndexPath - ios

I am facing a strange problem. I have a tableview with multiple selection enabled. In the cellForRowAtIndexPath method, I have the following code:
if getSelectionStatusForItemAt(indexPath.row)
{
cell.selected = true
self.tblPayments.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
}
return cell
After reload data, single/multiple cells are selected. The indexPathsForSelectedCells method also returns a non empty array. But, the problem is that when I touch one of these cells again, the didSelectRowAtIndexPath method is called instead of didDeselectRowAtIndexPath.
I think I am missing something in the way tableview delegates work.

When you call self.tblPayments.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None) you are selecting the row, but it remains selected until you unselect it and that's why didSelectRowAtIndexPath is called and didDeselectRowAtIndexPath is not.
To call didDeselectRowAtIndexPath after you select the cell you should, in didSelectRowAtIndexPath call tableView.deselectRowAtIndexPath(indexPath, animated: true). Another option is to do your operations at didSelectRowAtIndexPath if possible.

Related

How to call didDeselectRowAt of tableView in a collectionView

I have a collectionView and a tableView. When I toggle a cell on tableView, it adds/removes that cell to collectionView. And remove a cell from collectionView when I click on it. I want to call didDeselctRowAt of tableView when clicking on cells of collectionView.
tableView(tableView, didDeselectRowAt: indexPath) // in collectionView
The above method is working. However, When I click tableView' cell again this method calls again. Is there any property like
cell.isDidDeselect = true // cell of tableView
To prevent calling again it.
Moved solution from question to answer:
Update and Answer
I found answer:
use
tableView.deselectRow(at: indexPath, animated: true)
tableView.delegate?.tableView!(tableView, didDeselectRowAt: indexPath)
instead of
tableView(tableView, didDeselectRowAt: indexPath)

Deselect a Table View Cell that has been selected programmatically

I am implementing a search feature where you can select results and I'm trying to make it so that it remembers which cells were selected if they show up in the results again.
I'm using cell.setSelected(true, animated: false) in the willDisplayCell function. However, now there is no way to deselect the cells. When tapped on again, neither the didSelectRowAtIndexPath nor the didDeselectRowAtIndexPath functions are being called. What can I do?
Try to use tableView.selectRowAtIndexPath(indexPath: NSIndexPath?NSIndexPath?, animated: Bool, scrollPosition: UITableViewScrollPosition) to select cells. Then you'll get deselect events.

UITableViewController cell remains highlighted when segueing back

I have a UITableViewController and when a cell is selected, my app segues to a new view controller. When I press the back button in my navigation controller to go back to the previous tableview controller, the cell that I had selected remains highlighted. I checked clearsSelectionOnViewWillAppear in viewWillAppeaer and it is true. Any idea why this is happening?
Are you calling the segue from didSelectRowAtIndexPath if so try something like this
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("Your Segue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
You could also place tableView.deselectRowAtIndexPath the in the prepareForSegue method which is what I tend to do

iOS UITableViewCell needs to be pressed twice to call didSelectRowAtIndexPath

I have a UITableView that sometimes requires you touch it twice to select a cell.
More specifics:
Two touches are needed only after the table has been scrolled all the way up or all the way down.
Only the second touch even calls didSelectRowAtIndexPath.
When the table opens in the natural "scrolled up position", cells are indeed selectable with just one touch.
If you scroll just a little bit (not all the way down/up), the cells will select with just one touch.
If cells do not fill the whole table and scrolling is not required, it works fine.
Go all the way to the top or bottom and you have to touch twice.
I have a feeling that the first touch is really making the UITableViewCells selectable or is activating the table in some way.
Things I have checked:
My code definitely doesn't call didDeselectRowAtIndexPath anywhere.
No UIGestureRecognizers are using setCancelsTouchesInView:.
Other settings on the table:
self.tableView.scrollEnabled = YES;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.bounces = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
What's causing this?
Update
Oddly enough, setting self.tableView.bounces = YES; fixed the problem.
I am still looking into the root cause in case anyone has a better answer. Obviously I would like for the table not to bounce, but not if it costs key functionality.
May be you implemented didDeselect instead of didSelect?
Swift:
Once a cell gets tapped, it also gets selected, try deselecting it as soon as it was tapped.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
Other things to check also are delaysContentTouches and canCancelContentTouches properties.
Swift 4
You need to surround your updates with beginUpdates() and endUpdates()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// get current cell
let cell = tableView.cellForRowAtIndexPath(indexPath) as! YourCustomCell
// change the value
// Update the tableView
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()
}
Maybe you should try to disable delaysContentTouches:
tableView.delaysContentTouches = NO;

UIDatePicker in UITableView, ensure Visibility

I have a static UITableView with several sections. In one table row I have a UIDatePicker. On touch the table cell expands and I can select the date. Fine so far. But if the table row is on the bottom of the page I need to manually scroll up to select a date. How can I ensure the datepicker to be in view like the calendar app does? Can you please point me into the right direction?
You can use this function:
func scrollToRowAtIndexPath(indexPath: NSIndexPath, atScrollPosition scrollPosition: UITableViewScrollPosition, animated: Bool)
Use it in
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// ...
var indexPathToJump = NSIndexPath(forRow: 0, inSection: 5)
tableView .scrollToRowAtIndexPath( indexPathToJump, atScrollPosition: .None, animated: true)
}
}
Use scrollToRowAtIndexPath:atScrollPosition:animated::
self.tableView.scrollToRowAtIndexPath(myIndexPath, scrollPosition: .None, animated: true)
I haven't tested this syntax, please let me know if it needs improved, but I know that the method is right. You want to use UITableViewScrollPosition.None so that it move the table view just enough that the row in question is in view:
UITableViewScrollPositionNone
The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by UITableViewScrollPositionTop. This is the default.
Available in iOS 2.0 and later.

Resources