Error deleteRowsAtIndexPaths from UITable - objective-c

I am having a big issue trying to delete from an UITableView.
Just for note, I have the same code running fine on another view and thats why it's making me crazy.
The only difference is that on the other view, my array is in a property. But I tried changing my tmpArray to a property and nothing changed. Here's the code and the error after:
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tmpArray removeObjectAtIndex:indexPath.row];
[tbvPlaylist deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Error:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
Let me now if you need more info.

Is tmpArray the same ivar that is providing the count of rows e.g.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return tmpArray.count;
}
Also at the moment you call
[tbvPlaylist deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
tableView:numberOfRowsInSection: should return the
numberOfRowsBeforeDelete - numberOfRowsBeingDeleted

Related

How to enable swipe to delete on custom UITableViewCell Objective-C

I need to add the "swipe to delete" feature on my UITableView, I tried with the following code:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[indexProdotti removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
NSLog(#"Unhandled editing style! %ld", (long)editingStyle);
}
}
The problem is that there is only a limited space I have to swipe in order to show the delete button, the rest of the cell if swiped gives no result.. Am I missing something in my implementation?

NSFetchedResultsController crashes on objectAtIndexPath?

I'm getting a crash when trying to access an object in NSFetchedResultsController.
2013-11-10 15:15:06.568 Social[11503:70b] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds for empty array with userInfo (null)
2013-11-10 15:15:06.570 Social[11503:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds for empty array'
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.resultController = [DataEngine sharedInstance].postsFetchedResultController;
self.resultController.delegate = self;
[self.resultController performFetch:nil];
[self.tableView reloadData];
[[DataEngine sharedInstance] fetchInBackground];
}
Result Controller Delegate
#pragma mark - NSFetchedResultsControllerDelegate Methods -
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeMove:
[self.tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
break;
default:
break;
}
}
table view
#pragma mark - UITableView Delegate & Datasrouce -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.resultController.fetchedObjects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self cellForRowAtIndexPath:indexPath];
}
One reason for crash on below line could be that you are updating self.resultContrlller after you have used it for retuning the number of rows of your table section. Make sure that if you are constantly updating your self.resultContrlller object then you take a copy of it and then use for your table drawing.
// This code does not prevent crash. Changing your regular code to modern objective-C way
Post *post = [self.resultContrlller fetchedObjects][indexPath.row];
The data source should not change while table is reloading itself. You should use a copy of fetchedObjects instead to load the table. So, evey time, before you reload your table, take a copy [[self.resultContrlller fetchedObjects] copy] and the use it for table drawing. Your main source can then keep on changing. And after table reload is done with copy you may want to reload it it again if there was a change in the data. Such crashes happens when your data source changes faster than table reloads.
In one of my NSManagedObject subclasses I had the follwiing code which was causing the issue. NSSets are automatically initialized on NSManagedObejcts and there is no need to initialize them.
- (void)awakeFromFetch
{
if (!self.comments)
self.comments = [NSMutableSet set];
if (!self.medias)
self.medias = [NSMutableSet set];
}
Another problem was that during insert indexPath is null, I had to use newIndexPath instead
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:#[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;

Prevent first row being deleted in table view

I want to stop all the rows being deleted from my table view by user so at least one row is always present.
Im still geting to grips with programming and objective c.
Heres what I tried:
if (indexPath.section != 0 | editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.circuits removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
then
if (indexPath.section > 1 | editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.circuits removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
That had no effect in that I can still delete the first row. So then I tried
- (BOOL)tableView:(UITableView *)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
return NO;
return YES;
}
Still cant prevent the first row from being deleted
You need to implement the UITableViewDataSource method tableView:canEditRowAtIndexPath: and return NO for the indexPath.row 0.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row > 0;
}
Try moving your code into
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Try this code :-
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
return NO;
else
return YES;
}
It will surely help you

Delete a cell row in Objective-C

Want to delete a row when you swipe with your finger on the cell.
I got this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Delete");
}
After running the Delete button appears but when I press on it nothing happens. What do I need to insert in to the brackets?
And my .m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSLog(#"U selected %#", str);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataSourceArray removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
//remove the corresponding object from your data source array before this or else you will get a crash
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.array = #[#"test",#"test"];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:nil action:#selector(updateArray) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
Try this,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.array removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
//remove the corresponding object from your data source array before this or else you will get a crash
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

Unable to delete custom table view cell

In one of my ViewControllers, I added a tableview as a subview.
In this tableView I made implemented custom table view cells.
So, how to delete the custom Table View Cell from tableView, when we press the delete button.
Can anyone help me out..
Try this,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[tableView beginUpdates];
[self.itemTable removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}