UITableView scrolls to top when inserting more rows - ios

In my code, I've overridden tableView:willDisplayCell, and when the last row is shown, I start a load for more data. When this data comes in, I call insertRowsAtIndexPaths to add the rows.
This generally works (the tableview's scroll position doesn't change), but I've noticed that if I overscroll and not let go (scroll past the end of the tableview) that the tableview will jump to the top when insertRowsAtIndexPaths is called upon data load.
I'm using UITableViewAutomaticDimension/Autolayout for my cell heights, as each cell has different heights depending on the data they contain. How can I avoid this issue? Ideally if the data loads and the tableview is over scrolled, the scroll position would stay at the overscroll once the data comes in.

You can create a reference on the bottom cell indexpath, and in whichever function is causing it to go back to the top, call
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
SWIFT 3
self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.none, animated: true)

Related

UITableview not scrolling to the bottom cell if using UITableViewAutomaticDimension and estimatedRowHeight

I am facing a strange issue with UITableView if I enable the UITableViewAutomaticDimension property.
I am fetchign some data from the server and I have kept my UITableView cell set to accept dynamic content and increase the height as per the content so far what i have done is
Set the number of lines of the UILabel to zero so that it grows as per the content
Added the below code inside the viewDidLoad method
self.myTable.estimatedRowHeight = 200
self.myTable.rowHeight = UITableViewAutomaticDimension
Now after i fetch the data from the server i reload the table and go to the last cell of the table view but my problem is when i reload my entire table after the webservice call,
the table view animates and scrolls to the top of the screen and is not visible unless I scroll it down.
I searched and googled but i was not able to find any solution to this but if any of you have every came across this kind of situation please guide me out.
I have even added the below line of code before returning the cell in the cellForRowAtIndexPath but still am stuck with the same issue
cell.layoutSubviews()
here's the code which i have added to scroll to the bottom cell after the successful service call
self.myTable.scrollToRowAtIndexPath(NSIndexPath(forRow: self.myArray.count - 1, inSection: 1), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

Edit on last cell of the TAble view

In my table view cell have textView.it is custom cell.
when i select one text view it move to top position of the tableview then i can edit.
[self.tableViewEdit setContentOffset:offset animated:YES];
this is the code i used for that.it work fine.
Last cell also move to top.but when i type in last cell it move to bottom of the last cell and move up again.
is there any way to remove last cell's bottom attraction with tableview.or how to fixed last cell top of the tableview.
when typing i used
[self.tableViewEdit beginUpdates];
[self.tableViewEdit endUpdates];
otherwise i cant keep keyboard when i typing.why i`m using above code i want to change cell height according to my typing.

UITableView does not scroll up every time I reload a tableview section

I have a table with multiple section headers. What I am trying to do is every time I click on one of the section headers, section and section rows get updated using following code
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationNone];
Now, the problem is sections are reloaded and they come up but the tableView does not scroll up on that. Say my header section that I want to expand is at the bottom of the visible screen, every time I click on section header, it expands below that and is not visible to the user until and unless user scrolls the screen himself. I want the view to scroll up so that user could see the expanded view on the screen.
Any ideas how can i do that or any sample code would be appreciated.
Thanks
Vik
Use the scrollToRowAtIndexPath:atScrollPosition:animated: table view method to scroll the table as needed.
[self.tableView scrollToRowAtIndexPath:someIndexPath atScrollPosition: UITableViewScrollPositionTop animated:YES];
Provide the appropriate indexPath. There are other options for the scroll position if 'top' isn't quite what you want.

iOS dynamically load new cell to the bottom of a tableview

I'm working on an iOS project where I want to dynamically add more cell to the bottom of the tableview once the user reaches the bottom. I currently have this working by calling a method that fetches more data that is added to the array that I'm using to hold my cells. When I'm done adding the objects to the array I call [tableView reloadData]. This works, but it doesn't visually load the cell until the tableview stops scrolling.
I also tried using [tableView insertRowsAtIndexPaths: inSection:withRowAnimation: UITableViewRowAnimationNone]. But I was getting very strange behavior where random cell would disappear and not show back up until I scrolled that section of the tableView off screen and then back to that area.
Is there a way to get the tableView to reload even if the tableView is still in mid scroll when the [tableView reloadDate] is called?
Thank you!
Check out: How to know when UITableView did scroll to bottom in iPhone
neoneye's answer about - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { was exactly what I was looking for. I moved the method call to fetch more cell right after the NSLog(#"load more rows");
Thank you JiaYow for steering me in the right direction!

how to change height cell in uitableview without reload data

I use UITableView and I want change the height of different cells. But I don't want use reload data for this because my table view contains UITextView which I edit in now and UIKeyBoard is up.
You're looking for the reloadRowsAtIndexPaths: method on the UITableView. It will trigger the recalculation of height (and subsequent redraw) of specific cells. It can even animate the adjustment so that you can do the update in real-time as the user is scrolling, such that the table view does not "jerk around" or "stutter step" while the heights are recalculated. I just implemented it successfully to do what you described.
Source:
Update UITableViewCell without reload
You can call beginUpdates && endUpdates, These will causes table cell heights to be recaluclated,
without reloading the entire cell
UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.endUpdates()
// Re-enable animations
UIView.setAnimationsEnabled(true)

Resources