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

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.

Related

UITablleView insert rows animated without scrolling

I have implemented a TableView with multiple sections and a section headers and section footers in each section. When the user tapes one cell more cells are inserted in an animated fashion.
To implement this I update the data model and call
[self.tableView insertRowsAtIndexPaths:addedIndexPaths withRowAnimation:(UITableViewRowAnimationTop)];
On the top rows this works as expect: The aded rows animate and the content ofset of the tableview is constant. On the last cell the tableview scrolls either to the top of the previous section or to the top of the footer of the previous section.
Why does the tableview scroll when I insert rows? How can I prevent that scrolling while keeping the animation?
The main way around this is to not use insertRowsAtIndexPaths. Instead try using reloadSections in order to keep the animations.

UITableView scrolls to top when inserting more rows

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)

UICollectionView Horizontal Scroll after scrollToItemAtIndexPath:

I have an array of images that is displayed in a collectionView similar to Instagrams profile page. When an image is selected, the indexPath.row gets passed to the next collectionView in which I use the same array and the passed indexPath.row to know what image to display on the entire screen.
- (void)viewDidLayoutSubviews{
[self.view layoutIfNeeded];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedImage inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
The image comes up but I am unable to scroll horizontally between the rest of the images. Paging is enabled and each cell is large enough to take up the screen. If I comment out the above code, I am able to scroll through all the images. So pretty much how Twitter functions when you select a persons image and are able to scroll through the rest instead of x'ing out and selecting each one individually
I believe viewDidLayoutSubviews is called whenever a collection view cell is reused. So when it tries reusing the cell, it runs the code that forces it to scroll back to that item, meaning that the scroll view never moves :) I think you need to make sure that scroll to the item only gets called once.
To test this out... try putting in a breakpoint in the code you have above, and see if this is getting called again as you try scrolling horizontally.

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.

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!

Resources