UITableView insertRowsAtIndexPaths:WithRowAnimation without freeze UI - ios

I try to understand what is the best practice to use when I work with UITableView with large number of row to insert when the table is visible.
This is my behavior:
I have one UITableView and one Thread that try to insert a data into this table. I think that make a [UITableView reloadData] is a poor solution for the performance aspect, and I know that UIKit operation are be carried on main thread, for this reason when the datasource update is completed I tried to send a NSNotification to make a UITableView update operation (beginUpdate - insertRowAtIndex - endUpdate) on the main thread, but this technique freeze the user interface. I have to work with 1000+ number of rows.
Someone has already solved this problem ? Is it a possible solution the use of GDC async and sync? If yes, how?
Thanks in advance.

Do you try like this:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView beginUpdates];
[self.tableDatalist insertObject:obj atIndex:index];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
});

Related

iOS: Should I use a delegate or an NSNotification in this situation?

I have a database that updates with new data constantly. I use this data as my datasource for my UITableView. Currently, I am using NSNotifications to alert my UITableView to insert, delete or update new data. However, I've been thinking it would be much better to use delegates because it is one on one.
Here is some code to better demonstrate what is going on.
- (void)insertObject:(NSNotification *)notification {
NSNumber *object = [notification object];
[self.tableView beginUpdates];
[self.data insertObject:object atIndex:0];
[self.tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:0] withAnimation:NSTableViewAnimationEffectNone];
[self.tableView endUpdates];
}
In a larger scope, database updates are typically an event that many parts of your project could be interested in. This is why Core Data itself uses notifications, rather than a delegate approach. Yes, you could go either way, but I would tend to follow Apple's lead on this pattern. (maddy's comment to your post does have an excellent explanation btw)

Scrolltorectatindexpath will not be able to scroll to particular section?

I had an application in which i want to scrolltoindexpath to a particular section. I am doing like this
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:anIndex] atScrollPosition:UITableViewScrollPositionTop animated:NO];
[table reloadData];
all working fine.but here the issue is tableview always kept to the bottom .
it won't scroll up after that.it will always keeping the last cell to the bottom.not scrolling up after that.I set the constraints as (0,0,0,0).
I think i miss something here .Can anybody guide me on this?
There are mainly 2 issues here.
1)
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:anIndex] atScrollPosition:UITableViewScrollPositionTop animated:NO];
With animated false, you have to forcefully call dispatch_get_main_queue() even if it's running on main thread. This is supposedly an iOS bug.
2) When you are trying to scroll the tableView, it calls cellForRowAtIndexPath for the available cells. However the immediate next line is calling reloadData which tries to call the same dataSource method. So it might be conflicting with others. So I would recommend adding a small delay for [table reloadData];

UITableView insertSections Memory Leak

I have a table which content I fetch remotely while showing a modal "Loading..." head up display, then I present the table when the content is ready by inserting the sections with animation:
[self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0, tableSectionsCount)]
withRowAnimation:UITableViewRowAnimationFade];
Inspecting this in Instruments shows a footprint in the VM: CoreAnimation for about 2.78 MB. This footprint stays there forever!
When replacing the code above with a simple [self.tableView reloadData]; I have no memory leaks at all. Although it is just ugly to do reload the table without animation.
Also replacing the UITableViewRowAnimationFade with other animations or even with UITableViewRowAnimationNone does not help either.
Is it a known issue?!
try to add beginUpdates before inserting and endUpdates after inserting and tell me what will happen
[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, tableSectionsCount)] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
It is a simulator issue. No problems on the physical device!

When reloading a UITableView section, all sections flash

I'm using a UITableViewController, and am using:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionNumber] withRowAnimation:UITableViewRowAnimationNone];
to reload an individual section of the tableview (to animate the insertion of new cells). The issue is that all of the sections, and their cells, flash white briefly every time this call is made. This does not occur if I use
[self.tableview reloadData];
but does occur no matter which row animation I use.
I'm aware that I can use insertRowsAtIndexPaths:withRowAnimation:, but I currently have a race condition that doesn't allow me to use that. I will, at some point, fix that, but in the meantime I would like to know why all sections cells flash while I'm reloading a single section. In addition, if I can turn off the flash and just animate the insertion / deletion of cells, that would be ideal.
I believe that it flashes because when you reload a specific section, it still has to recalculate the size of all the visible sections. Hence the flash. Without using insertion/deletion (because it knows the table will only change by one), you can't get past that.
If you are just trying this in the simulator though, it is possible that the flash will either disappear or be less noticeable when loaded on an actual device.
May be it's too late. But it could help someone.
Obj-c:
[UIView performWithoutAnimation:^{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone];
}];
Swift 5:
UIView.performWithoutAnimation {
self.tableView.reloadSections(NSIndexSet(index: indexPath.section), with: .none)
}

deleteRowsAtIndexPaths without animation

I got an app with a UITTableView. This table is updatable. And when in the next update the number of rows are less than in previous version, i implement the UITableView's method - deleteRowsAtIndexPaths.
I do it in this way:
[table beginUpdates];
[table deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[table reloadRowsAtIndexPaths:indexPathsReload withRowAnimation:UITableViewRowAnimationNone];
[table endUpdates];
But when i implement this code there are animations. And this animation is really bad. A lot of black leaks. But i did write - withRowAnimation:UITableViewRowAnimationNone in both methods: delete and reload.
Why? How can i reload and delete rows without animation?
Usually the way this is done is by modifying the data source of the table, and calling reloadData on the table.
maybe you try to work with NSMutableArray (delete row by removeObjectAtIndex:indexPath.row) in TableView and reload the whole tableview by using [tableView reloadData] ?
You can try to make that "gesture" out of the table,and try reload table
Unlike what the name suggests *UITableViewRowAnimationNone* doesn't help at all.
Maybe setting the alpha on the cell works as suggested here. Not sure though.
problems with animation when deleting the last row of a TableView in ios7

Resources