UITablleView insert rows animated without scrolling - ios

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.

Related

UITableView: How to know when all the cells of TableView with dynamic cell heights are resized?

The problem I am facing is that when I send a data to UITableView then I send a command to scroll the table to bottom. If cell heights are constant then my scrollToLastCell takes to last cell but with dynamic cell heights it doesn't always takes to last cell.
I want to know is there any way when I can be sure that all cells of my tableView is finished resizing and then I trigger scroll to bottom?
In the Docs Apple states
Important
If your table view includes self-sizing cells, headers, or footers,
you must provide estimated heights for those items.
https://developer.apple.com/documentation/uikit/views_and_controls/table_views/estimating_the_height_of_a_table_s_scrolling_area
Given that, I'd suggest you to go ahead and use tableView(_:estimatedHeightForRowAt:) method

Why does my tableview automatically scroll after reloadData or reloadSection?

I am using UITableViewAutomaticDimension in estimatedHeightForRowAtIndexPath. My tableViewCells are having large content in it. When i try to reloadData or reloadSection it automatically scroll up or down. I can not use reloadRow as my action can cause insertion/deletion/reload of certain number of rows in section.

TableView reload not setting UITableView section height

I am working on a UITableView which has 2 sections, both of which have exactly 1 row which is a custom cell. This custom cell has a collection view which is used to display various products.
Specific to my issue, the two collection views have 6 cells(three rows in the collection view). However when I load the page, the second section only displays 4 cells (two rows in the collection view). I am using UITableViewAutomaticDimension for the tableview which has the cells that contain these collection views.
I have tried using setLayoutNeeded and layoutIfNeeded, I am calling reload data on the tableView in a number of places. I am reloading the second section specifically as well. However the only way the cell's dimensions get fixed is if I scroll down to the second section, then scroll up to the first and then go back to the second.
I have been stuck on this for the past 3 days and absolutely any help would be greatly appreciated.
However the only way the cell's dimensions get fixed is if I scroll
down to the second section, then scroll up to the first and then go
back to the second.
According to this, I think you should reload your collection views firstly. And then reload your table view.

Shows bottom section of table view at visible view

I know the title is confusing. So I explain what I expect.
I have 3 sections in my table view. The first section has one or more rows. And second and third sections has only one row. When the first section has more than 4 rows the next sections going to be visible by scrolling table view bottom.
But I need these sections be visible in this case.
Actually I need show these sections at the bottom of the visible frame when first section has more than 4 rows!
Is it possible ?
The thing you can do it to display only the 3 first rows of the first section and then when the user starts scrolling, you add the additional cells to the first section while he is scrolling until you've added all of your cell.
Do the opposite when scrolling to top.
You can use two different TableView instead of one.
declare first TableView with only one section & seconds TableView with two section with one row each.
for that you need to use UIViewController instead of UITableViewController.
in this way your bottom TableView Cell always stay visible while first TableView can scroll with more cells.

Making section in UITableView closed(with 0 cells) after scrolling away and returning to it

I have a UITableView where I have section headers that can be tapped to expand or collapse the section. In my particular example each section only has one row, which is either visible (section expanded) or hidden (section collapsed). (I have a UITapGestureRecognizer on the section headers which I use to expand or collapse the sections) here the process of my actions:
I'm tap on the section (this is UITableViewHeaderFooterView custom view) and i see the cell that appears under the section header. At this moment everything is going OK. But when i scrolling down my tableview, cell goes behind header (like in Contacts application , header is first letter of contact). And finally when i scroll enough to don't see that section with that cell on, and when i scroll up - to return to my display that section - i want to make this section automatically known that when i return(from scrolling away that section from view) to it - i want that section already was closed with 0 cells in it.
Maybe i must use -(void) prepareForReuse at my custom UITableViewHeaderFooterView, or scroll methods ? or any tableView methods?
Thank you
You can implement the scroll view delegate methods and use them to check the scroll position (contentOffset) and / or the visible cells (indexPathsForVisibleRows). Once you know that you can collapse any sections that are no longer visible. You should probably use one of the scroll methods that means the scrolling has stopped (maybe scrollViewDidEndScrollingAnimation:) because changing the table view content size (which changing the number of rows will do) during a scroll animation could cause it to stop abruptly.

Resources