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.
Related
The ScrollToRow is not working as I want.
The function I wrote seems to be working fine, but I want it to scroll down to last row only when its added like it happens in chat. When you get a new message, your tableview scrolls to the bottom to read new message.
However, my tableview seems to reload every time before performing scrollToLastRow function (what I mean is every time before scrolling to bottom... it first goes back to top first cell and starts scrolling to bottom from there Every time when a new cell is created).
My code is simple for the scroll:
func scrollToLastRow()
{
let indexPath = IndexPath(row: messages.count - 1, section: 0)
self.chat.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
To check my code:
here's a link to my repository
ChatApplication
the ViewController's used for chat is ---> chatVC
the ViewController used to make chat is -> chatCell
I have tried anything I could get all over I could find.
Tried dispatch (without it the scroll function wasn't working at all)
Used the function on many different places (including viewDidAppear)
Used dispatch.asyncAfter for delay.....
but nothing worked.
#Fahad, you are reloading the tableview before the scrollToLast function is getting called. Please check and please verify at your end.
Note please update your GitHub repository url in your question properly as it not clickable.
Use this function for scrolling to the bottom.
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
use tableview.scrollToBottom()
Use tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation) for adding object to end of the tableView.
From the problem you say and looking in to your code, the animation issues are due to reloading the entire tableView after a new item is added to dataSource array,
What you have to do is to use Call tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation) with the correct indexPath on your tableView and the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) will be invoked.
I've been trying to figure out how to make my collection view of 3 cells load with the 2nd cell, and I finally figured it out after looking through StackOverFlow. However, the code that I came across is a bit confusing to me. Would anyone be able to explain why this code below works in making my collection view cell (that covers the whole screen) start with the 2nd of 3 cells? (this is the effect I wanted to achieve all along, but I want to learn more about why this code works exactly.
In this block of code, there's a bool variable and an if statement, why are they needed? When I took out the boolean variable and if statement, the collection view was unable to scroll.
How does this block of code work exactly?
Thank you.
var onceOnly = false
internal override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !onceOnly {
let indexToScrollTo = IndexPath(item: 1, section: 0)
self.collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
onceOnly = true
}
}
1- This scrollToItem in Docs
let indexToScrollTo = IndexPath(item: 1, section: 0)
self.collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
makes the collectionView scroll to the second item in the list
2-
When I took out the boolean variable and if statement, the collection view was unable to scroll
Because willDisplay is called for every cell display , so when for example you scroll to 3rd cell willdisplay is called and causes the collectionView to go to the second cell , so it makes it stuck in the second item all the time ( and this seems like no scroll but the scroll happens and you won't notice that as it happens instantly ) , so the boolean var is needed to make that scroll action happens once which is the scroll to the specified index
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)
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
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.