Swift 3 - Scroll to top of Tableview on Tabbar Switch - swift

I have a few UITableViewController's set up as child-views of UINavigationController's that are all part of a single UITabBarController. One of these UITableViewController's updates a tableViewController that is part of a different UITableViewController on a button press, which then switches the currently selected tab via:
self.tabBarController?.selectedIndex = 1
I would like the tableView in the newly selected index, in this case 1,to scroll back to the top automatically on changing between the tabViews. I have tries simply adding :
self.tableView.setContentOffset(CGPoint.zero, animated: true)
To viewWillAppear() but this doesn't seem to work. If you scroll to the bottom of the tableView on index 1, then tab over to the tabBarController that updates that index(say index 0), update the index and get switched back to index 1, the tableview for index 1 remains on the same row as when you originally were viewing it. The data is properly loaded, but the actual UI is still on the row you left at. I would like to implement it so that switching between tabs automatically scrolls to the top.

You can try using IndexPath and scroll to index 0 of section 0 :
let indexPath = IndexPath(row: 0, section: sectionIndex) // set row = 0 and section = 0
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)

You can use scrollToRow(at:at:animated:).
You can add this to the viewWillAppear(_ animated: Bool) function:
let indexPath = IndexPath(item: 0, section: 0)
tableView.scrollToRow(at: indexPath, at: .top, animated: false)

Related

how to show the middle cell of a collection view when my app starts

I have a collectionView embedded in a subview of the ViewController. The collectionView has 12 cells. Each cell takes up the whole width and height of the collection view, so that I can achieve the pagination affect. However, when the app starts, I want to show the middle cell like the 6th or 7th one of my collectionView.
P.S. I have the collectionView in a wrapper view, not in my viewController.
In my WrapperView, I added the following method but as this method is called after the collectionView is added, it shows a sudden jump.
override func didAddSubview(_ subview: UIView) {
super.didAddSubview(subview)
let indexPath = IndexPath(row: 6, section: 0)
DispatchQueue.main.async {
self.calendarCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
}
}
If I could do it before the collection view appear on the screen, I may be able to fix that problem, but I can't find which method is called before the didAddSubview(_:) method in UIView Life Cycle.
Can anyone give me hint on how to solve this.
After your data source has loaded use:
func selectItem(at indexPath: IndexPath?,
animated: Bool,
scrollPosition: UICollectionView.ScrollPosition)
https://developer.apple.com/documentation/uikit/uicollectionview/1618057-selectitem
Probably best used before the view appears so trigger in viewWillAppear or viewDidLoad

Expandable tableView cell visible issue for bottom row?

I have the tableview cell with expandable row its working fine. Now I have the issues on when bottom cell of the table view row it will expand but user need move to tableview for viewing expandable part. how to do it will move automatically?
I think you are reloading the section after expand/collapse the 'tableView' sections.
You can scroll the tableView to the section rect you are expanding.
For ex:
let sectionRect = tableView.rect(forSection: section)
tableView.scrollRectToVisible(sectionRect, animated: false)
Hope this helps!
Better use the func scrollRectToVisible(_ rect: CGRect, animated: Bool) this method does not scroll the tableview if the section is visible. You can easy to get the section rect using this method func rect(forSection section: Int) -> CGRect
example:
tableView.performBatchUpdates({ [weak tableView] in
tableView?.insertRows(at: indexPaths, with: .fade)
}, completion: { [weak tableView] _ in
if let sectionRect = tableView?.rect(forSection: section) {
tableView.scrollRectToVisible(sectionRect, animated: true)
}
})

Swift tableView scroll to row gives a wrong value for the content Offset value of my tableview

In my application I have a tableview in which I am showing approximately 400 products. The problem I am facing is that while scrolling to the beginning of a section programmatically like so:
#objc func CategoryPress(_ button: UIButton) {
let indexPath = NSIndexPath(item: 0, section: button.tag)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.top, animated: false)
}
I am getting a wrong value for my tableView.contentOffset.y that stays incorrect until I reach the top of my tableview again. This issue happens only when scrolling to a position far from my start position.
While scrolling manually I am not facing this issue and all my values are exact.
I tried to scroll to a specific y on the button press but I didn't find a way to do this.
Any Solution or alternative to get the specific position would be highly appreciated.
I found a working alternative which is:
#obj func CategoryPress(_ button: UIButton) {
let indexPath = NSIndexPath(item: 0, section: button.tag)
var offset: CGPoint = tableView.contentOffset
offset.y = CGFloat(Ycoordinates[indexPath.section])
tableView.setContentOffset(offset, animated: true)
}

Swift UITableView scrollToRow not working

I have a tableView with multiple lines and textFields. Some of the textFields won't be visible when the keyboard is visible. So I want the selected row to scroll to top to be visible.
I'm using
let indexPath = IndexPath(row: index, section: 0)
tableViewActivity.scrollToRow(at: indexPath, at: .top, animated: true)
for this to scroll the selected row to top. This code is inside textFieldDidBeginEditing and is called correctly, but the tableView does nothing, there is no scroll. I found a post with a workaround for some by using a delay or reload the Data before scrolling, but nothing changed for me with this. So can anybody help me please?

Scroll to cell but no select

I have a UIViewController with a UIBarButtonItem "Edit".When I tap "Edit" I push another controller with some data.I have 2 UICollectionView inside my controller and they scroll to cells with a specific NSIndexPath.For example in this image:
It works if I can see cells (from 1.0/5.0 to 2.5/5.0) but if the item of my IndexPath is 6 for example, my collectionView scrolls to the 6th cell (4.0/5.0) but it doesn't select it, and I got this result(as you can see cell's background is not black, so it is not selected):
This is my code:
var index = NSIndexPath(item: indexPathSelected.item, section: 0)
ratingCollectionView.collectionView.selectItem(at: index as IndexPath, animated: true, scrollPosition: .centeredHorizontally)
ratingCollectionView.collectionView(collectionView, didSelectItemAt: index as IndexPath)