iOS - deleting row from UITableView doesn't animate - objective-c

I have a very frustrating issue. I have an app with a UITableView.
When I am removing a cell from the table view, it is removed from the data model and then I call the following:
-(void)removeItem:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
My problem is, I've tried it like I do above, and I've tried without using animateWithDuration. I've even tried with a CATransaction, but however I do it, the animation doesn't happen.
I've got slow animations on in my Simulator and when I remove an item from the list, it removes correctly, but without animation.
It just disappears and leaves a blank space for a moment before the table view data is reloaded.
I've search all over SO and Google and I can't seem to find an answer.
Any ideas?
Does it perhaps have to do with the fact that I'm removing the object from the data model before calling the function above?
Edit: Removed the Animation Block as it is incorrect

According to THIS you don't need to use the animateWithDuration:animations: at all.
Just try it like this
-(void)removeItem:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}

Okay, I don't know if it's just bad manners, but I feel I'm going to answer my own question here.
First, thanks for all the other answers, you were all correct of course, but none of the answers solved my problem.
It turns out that there is another area in the code that does a different check then calls one of the tableView delegate methods, that seems to cancel the animation.
So the answer is as follows:
When you're row is removing but the animations aren't working, make sure that you are not calling didSelectRowAtIndexPath:indexPath before the animation starts. This will cancel the animations.
If you're NOT having that problem, here's some really typical code for expanding, two lines in the example:
Note that facebookRowsExpanded is a class variable you must have:
if ( [theCommand isEqualToString:#"fbexpander"] )
{
NSLog(#"expander button......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray *deleteIndexPaths;
NSArray *insertIndexPaths;
facebookRowsExpanded = !facebookRowsExpanded;
// you must do that BEFORE, not AFTER the animation:
if ( !facebookRowsExpanded ) // ie, it was just true, is now false
{
deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
deleteRowsAtIndexPaths:deleteIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
else
{
insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
// DO NOT do this at the end: [_superTableView reloadData];
return;
}
NOTE: your code for numberOfRowsInSection must use facebookRowsExpanded
(it will be something like "if facebookRowsExpanded return 7, else return 5")
NOTE: your code for cellForRowAtIndexPath must use facebookRowsExpanded.
(it has to return the correct row, depending on whether or not you are expanded.)

First, you don't need to animate the change yourself.
Second, I think you need to make the changes to the datasource between the begin and end updates.
Your method should look something like this:
-(void)removeItemAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView beginUpdates];
// I am assuming that you're just using a plain NSMutableArray to drive the data on the table.
// Delete the object from the datasource.
[self.dataArray removeObjectAtIndex:indexPath.row];
// Tell the table what has changed.
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
indexPaths is an array of NSIndexPaths to be inserted or to be deleted in your table.
NSarray *indexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];

Related

fetchedresultscontroller delegate called with invalid indexpath when 1 managed object updated and another deleted [duplicate]

I have a UITableView that uses an NSFetchedResultsController as data source.
The core data store is updated in multiple background threads running in parallel (each thread using it's own NSManagedObjectContext).
The main thread observes the NSManagedObjectContextDidSaveNotification
notification and updates it's context with mergeChangesFromContextDidSaveNotification:.
Sometimes it happens that the NSFetchedResultsController sends an
NSFetchedResultsChangeUpdate event with an indexPath that does not exist
anymore at that point.
For example: The result set of the fetched results controller contains
1 section with 4 objects. The first object is deleted in one thread.
The last object is updated in a different thread. Then sometimes the
following happens:
controllerWillChangeContent: is called.
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: is called with
type = NSFetchedResultsChangeDelete, indexPath.row = 0.
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: is called with
type = NSFetchedResultsChangeUpdate, indexPath.row = 3.
But the fetched results controller contains only 3 objects now, and if call
MyManagedObject *obj = [controller objectAtIndexPath:indexPath]
to update the table view cell according to the NSFetchedResultsChangeUpdate
event, this crashes with a NSRangeException exception.
Thank you for any help or ideas!
I have now found a solution for my problem. In the case of an update event, there is no need to call
[self.controller objectAtIndexPath:indexPath]
because the updated object is already supplied as the anObject parameter to the -controller:didChangedObject:... delegate.
I have therefore replaced -configureCell:atIndexPath: by a -configureCell:withObject: method that uses the updated object directly. This seems to work without problems.
The code looks now like this:
- (void)configureCell:(UITableViewCell *)cell withObject:(MyManagedObject *)myObj
{
cell.textLabel.text = myObj.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCellIdentifier"];
[self configureCell:cell withObject:[self.controller objectAtIndexPath:indexPath]];
return cell;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
/* ... */
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] withObject:anObject];
break;
/* ... */
}
}
This is actually quite common because of the bug in Apple's boiler plate code for NSFetchedResultsControllerDelegate, which you get when you create a new master/detail project with Core Data enabled:
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
Solution #1: Use anObject
Why query the fetched results controller and risk using an incorrect index path when the object is already given to you? Martin R recommends this solution as well.
Simply change the helper method configureCell:atIndexPath: from taking an index path to take in the actual object that was modified:
- (void)configureCell:(UITableViewCell *)cell withObject:(NSManagedObject *)object {
cell.textLabel.text = [[object valueForKey:#"timeStamp"] description];
}
In cell for row, use:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self configureCell:cell withObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
return cell;
}
Finally, in the update use:
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
withObject:anObject];
break;
Solution #2: Use newIndexPath
As of iOS 7.1, both indexPath and newIndexPath are passed in when a NSFetchedResultsChangeUpdate happens.
Simply keep the default implementation's usage of indexPath when calling cellForRowAtIndexPath, but change the second index path that is sent in to newIndexPath:
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:newIndexPath];
break;
Solution #3: Reload rows at index path
Ole Begemann's solution is to reload the index paths. Replace the call to configure cell with a call to reload rows:
case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
There are two disadvantages with this method:
By calling reload rows, it will call cellForRow, which in turn calls dequeueReusableCellWithIdentifier, which will reuse an existing cell, possibly getting rid of important state (e.g. if the cell is in the middle of being dragged a la Mailbox style).
It will incorrectly try and reload a cell that isn't visible. In Apple's original code, cellForRowAtIndexPath: will return "nil if the cell is not visible or indexPath is out of range." Therefore it would be more correct to check with indexPathsForVisibleRows before calling reload rows.
Reproducing the bug
Create a new master/detail project with core data in Xcode 6.4.
Add a title attribute to the core data event object.
Populate the table with several records (e.g. in viewDidLoad run this code)
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
for (NSInteger i = 0; i < 5; i++) {
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:[NSDate date] forKey:#"timeStamp"];
[newManagedObject setValue:[#(i) stringValue] forKey:#"title"];
}
[context save:nil];
Change configure cell to show the title attribute:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%# - %#", [object valueForKey:#"timeStamp"], [object valueForKey:#"title"]];
}
In addition to adding a record when the new button is tapped, update the last item (Note: this can be done before or after the item is created, but make sure to do it before save is called!):
// update the last item
NSArray *objects = [self.fetchedResultsController fetchedObjects];
NSManagedObject *lastObject = [objects lastObject];
[lastObject setValue:#"updated" forKey:#"title"];
Run the app. You should see five items.
Tap the new button. You will see that a new item is added to the top, and that the last item does not have the text "updated," even though it should have had it. If you force the cell to reload (e.g. by scrolling the cell off the screen), it will have the text "updated."
Now implement one of the three solutions outlined above and in addition to an item being added, the last item's text will change to "updated."

UITableView deleteRowsAtIndex & datasource troubles

Happy 14th Baktun everybody!
This thing has been bugging me for a while, and I've googled to and fro, and also checked SO for an answer, but I just can't seem to grasp the answers and apply it to my app. If some of you would be so kind to help out, that would be appreciated!
I got the following function that deletes a cell from an UITableView when a stepper reaches 0.
I want the cell to be animated to the left, so when you press the - sign you'll see the cell swipe out of view. I set the sender.tag to the [[cell stepper]setTag:indexPath.row]; in CellForRowAtIndexPath.
- (void)stepperChanged:(UIStepper *)sender
{
WinkelWagen *ww = [WinkelWagen sharedWinkelWagen];
BWBand *band = [ww.winkelWagenArray objectAtIndex: sender.tag];
NSDecimalNumber *nummer = [[NSDecimalNumber alloc]initWithFloat:sender.value];
band.winkelmandjeAantal = nummer;
[sender setValue:[band.winkelmandjeAantal doubleValue]];
// Remove the cell and object from WinkelWagen if stepper.value turns 0
if (sender.value == 0)
{
NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:sender.tag inSection:0],
nil];
[ww.winkelWagenArray removeObject:band];
[self.tableView beginUpdates];
[self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationNone];
if ([winkelmandjeData count] == 0)
{
NSArray *addIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
nil];
[self.tableView insertRowsAtIndexPaths:addIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
}
UPDATE:
I've worked it out with [[self.tableview]reloaddata] but then the animation doesn't play. Can someone tell me how I should handle the data and the animation properly?
Thanks!
I see there something you could have a problem with, but don't know if it is an answer to your question. In your if ([shoppingCartData count] == 0) you are adding a row to the table, but the data source remains the same. That could result in a crash.
Also, before the if, your data source seem to be ww.shoppingCartArray, but in the if you are checking the count of shoppingCartData.
Hope this helps!

Select uitableview row programmatically

i have read a lot on this argument, i have tested this example that works:
source code example. but this doesn't use storyboards (i don't know if this is the reason that make my project not working)
i have made another project using storyboard, the same sequence of instruction doesn't work...
-(void)viewDidAppear:(BOOL)animated{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[firstController.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[firstTable.delegate tableView:firstTable didSelectRowAtIndexPath:indexPath];
}
here is my project, is the same of the previous link, only adding viewDidAppear
my not working project
can someone tell me what's wrong?
thanks
I tried to study your code downloading the project.The problem is that firstController.tableView is nil.
So fix it:
[firstTable selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
Or assign firstController.tableView to the table view.
- (void)viewDidLoad {
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
If you want the callback from the delegate:
if ([self.tableView.delegate respondsToSelector:#selector(tableView:didSelectRowAtIndexPath:)]) {
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

didSelectRowAtIndexPath event not triggered when select row programmatically

I would like to select a cell in a tableView. It is working properly using this code, but once selected programmatically it does not trigger a didSelectRowAtIndexPath event. How can I trigger it?
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
It is working as documented:
Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.
Call it yourself after selecting your cell:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:self.mainTable willSelectRowAtIndexPath:indexPath];
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.mainTable didSelectRowAtIndexPath:indexPath];

Add/Delete UITableViewCell with animation?

I know this might sound like a dumb question, but I have looked every where. How can I do this?
I know how to do this with a swype-to-delete method, but how cam I do it outside that function?
Please post some code samples.
Thanks!Coulton
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
insertIndexPaths is an array of NSIndexPaths to be inserted to your table.
deleteIndexPaths is a array of NSIndexPaths to be deleted from your table.
Example array format for index paths :
NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];
In Swift 2.1+
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade)
tableView.endUpdates()
And you can create an NSIndexPath easily like so:
NSIndexPath(forRow: 0, inSection: 0)
You want these two methods: insertRowsAtIndexPaths:withRowAnimation: and deleteSections:withRowAnimation: They are both detailed in the UITableView documentation.
Swift 4.0
tableView.beginUpdates()
tableView.deleteRows(at: [YourIndexPathYouWantToDeleteFrom], with: .fade)
tableView.insertRows(at: [YourIndexPathYouWantToInsertTo], with: .fade)
tableView.endUpdates()
For creating IndexPath use this:
IndexPath(row: 0, section: 0)