How can a UITableView be navigated with arrow keys (physical keyboard) much like the spotlight behavior that was introduced in iOS 10?
Looking to have a user type into a search box and use the up/down arrow to highlight a row in a tableview and press return key to select the row.
I have not found any resources on the topic.
Please provide sample code in Swift.
Did not try it, but this could be the answer for you.
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
and use the text field delegate to handle return.
Edit: For highlighting use
tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
and update your cell visual
Related
When a tableview cell is selected in my app, it remains selected until a different cell is selected.
How can I make the highlighting of the tableview cell fade out after a second or two similar to Apple's Find My Friends app and the Find My iPhone app. When switching the distance from In Miles to In Kilometers you can see that the cell is highlighted and then the highlight fades out.
Surely you searched for an answer first?
Anyway...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
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 have a UITableView that has multi selection enabled. I have been using the "selection" to actually change the height of the rows, showing extra detail when "selected". E.g.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return (self.tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false) ? 200 : 92
}
This seems to work pretty well. Until I start doing any swipes actions. When I add some swipe actions, the swipe action seems to clear all of my selections. I actually wanted to deselect the one I was swiping, so it would shrink back down. But the clearing of all my selections doesn't seem to trigger any of the normal delegate callbacks. Even though I have allowsMultipleSelectionDuringEditing set to true.
Is there a way to do this? Should I skip (ab)using the selection state as a way to indicate whether the row is showing details with a different height or not? Or is there a way to use it in conjunction with the behavior of the swipes being done in "edit mode" and clearing all of my selections?
The best way is using NSArray to store indexPath of selected cells, and base on saved indexPath you can check and do anything you want. another bug may happened in your code is: What happened in the case user make scroll on tableview? Does cell will reuse and lose select state? New cell reuse the old cell with 200 height will has wrong height?
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;
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.