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.
Related
What I want to achieve you can see in the native clock app.
When you select a ringtone the cell gets the checkmark accessory and highlights on touch, but it unhighlights directly afterwards with a fade animation.
So my question is, is there a way to use temporarily native cell highlighting via the selectionStyle of my UITableViewCell instance AND keep the selection afterwards to show the accessory view.
I know that I can just write tableView.deselectRow(at: indexPath, animated: true) in the didSelectRowAt delegate method. But this removes the ability to bind the checkmark to the selection state.
I also know that I can animate the background of the cell on my own, but this is bad since iOS could change the default highlight color and I want my app to feel as native as possible with as less custom UI code as possible.
Is there a native way that I'm missing here?
Theres two options I can think of.
Easiest but loses the unhighlight animation.
Set allowsSelection to true on your tableview and override setSelected on your cell to do this.
override func setSelected(_ selected: Bool, animated: Bool) {
accessoryType = (selected ? .checkmark : .none)
}
It will highlight the cell when the user presses it, then show a checkmark.
More complicated but keeps the animation.
Don't use the allowsSelection property on the tableview and track the selected row yourself.
In cellForRowAt: you need to set the cell to show/hide the checkmark.
In didSelectRowAt: you need to remove the checkmark from the old selected row, update your value tracking the selected row, and add the checkmark to the new selected row.
I'm new in IOS development and I'm developing an app that uses a tableview. Within the tableviewrow I have a UICollectionView to display items in both directions(horizontal and vertical). The problem comes when I try to scroll to the last item in UICollectionView which is non visible and I want to give this item the focus and set it visible.
** cell = UITableViewCell
** tableCell = UICollectionView
I have tried this but does not work:
cell.tableCell.scrollToItem(at: IndexPath, at:.bottom, animated: true)
Please somebody help me!!
Thank you
The 2nd parameter for the scrollToItem function is not of type IndexPath.
open func scrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionViewScrollPosition, animated: Bool)
The type is UICollectionViewScrollPosition. So, the code sould be something like:
cell.tableCell.scrollToItem(at: IndexPath, at: UICollectionViewScrollPosition.bottom, animated: true)
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.
How can I reset cell selections in a UITableView?
Use case:
I have a ViewController with a UITableView. I select a cell and I enter another View Controller. When I come back to the TableViewController, I still see the selected cell. I would like to reset any selection.
In:
override func tableView (tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath)
You could do the following:
tableView.deselectRowAtIndexPath(indexPath, animated:false)
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.