‘Invalid update: invalid number of rows in section 0

You need to remove the object from your data array before you call deleteRowsAtIndexPaths:withRowAnimation:. So, your code should look like this: // Editing of rows is enabled – (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //when delete is tapped [currentCart removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } You can also simplify … Read more

NSMutableArray – force the array to hold specific object type only

Update in 2015 This answer was first written in early 2011 and began: What we really want is parametric polymorphism so you could declare, say, NSMutableArray<NSString>; but alas such is not available. In 2015 Apple apparently changed this with the introduction of “lightweight generics” into Objective-C and now you can declare: NSMutableArray<NSString *> *onlyStrings = … Read more

How to sort NSMutableArray using sortedArrayUsingDescriptors?

To sort your array of objects you: setup NSSortDescriptor – use names of your variables as keys to setup descriptor for sorting plus the selector to be executed on those keys get the array of descriptors using NSSortDescriptor that you’ve setup sort your array based on those descriptors Here are two examples, one using NSDictionary … Read more

How do I sort an NSMutableArray with custom objects in it?

Compare method Either you implement a compare-method for your object: – (NSComparisonResult)compare:(Person *)otherObject { return [self.birthDate compare:otherObject.birthDate]; } NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)]; NSSortDescriptor (better) or usually even better: NSSortDescriptor *sortDescriptor; sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”birthDate” ascending:YES]; NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDescriptor]]; You can easily sort by multiple keys by adding more than one to … Read more