Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
maytelabargamalaga
Courses Plus Student 6,465 PointsCrash when I try to delete rows from tableview
Hello everybody: I hope someone can help me. I want to try to delete row from my tableview with commitEditingStyle method but allways crash.
the crash message: 2014-10-15 22:09:53.052 listoDelivery[1914:228129] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318/UITableView.m:1582 2014-10-15 22:09:53.055 listoDelivery[1914:228129] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Here is my code:
#import "listadoSnacks.h"
#import "editSnacks.h"
#import "editSnacksTableViewCell.h"
@interface listadoSnacks ()
@end
@implementation listadoSnacks
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.mipedido = [[PFUser currentUser] objectForKey:@"mipedido"];
PFQuery *query = [self.mipedido query];
[query orderByAscending:@"nombreProducto"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
self.allProducts =objects;
[self.tableView reloadData];
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.allProducts count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier =@"editSnacksTableViewCell";
editSnacksTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"editSnacks" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
PFUser *user = [self.allProducts objectAtIndex:indexPath.row];
cell.precio.text =user [@"precio"];
cell.nombreProducto.text =user[@"nombreProducto"];
return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
PFUser *user = [self.allProducts objectAtIndex:indexPath.row];
[_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]];
[[PFUser currentUser] saveInBackground];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showPedidos"])
{
editSnacks *viewController = (editSnacks *)segue.destinationViewController;
viewController.allProducts= [NSMutableArray arrayWithArray:self.allProducts];
}
}
@end