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

iOS

Please, who can help me????? Deleting a row!!!!

Please who know, where is my mistake? I try to delete a row and I'm supposed to stop the app and then to restart to reload data in my tableview. After I restart the app, the row is deleted.

There is my code:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

    PFObject *message1 = [self.messages objectAtIndex:indexPath.row];


    [message1 deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                message:@"The item can't be deleted."
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];

        }


        else {

            [message1 saveInBackground];

            [self.tableView reloadData];

        }

    }];

} [self.tableView reloadData];

}

7 Answers

I dont think you need to save the message after deleting it. remove the line that saves it in the else block

 [message1 deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                message:@"The item can't be deleted."
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];

        }


        else {


            [self.tableView reloadData];

        }

Dear Preston thank you very much for your answer. But still the same problem, I need to do a refresh to disapper the row. I just want when I tap on delete button the item disappear immediate. Maybe there is a method???? Thank you again!!!!

you need to call the delete rows at indexPath method:

if (editingStyle == UITableViewCellEditingStyleDelete) {

    //animates the deletion of the row
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    PFObject *message1 = [self.messages objectAtIndex:indexPath.row];


    [message1 deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                message:@"The item can't be deleted."
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];

        }


        else {

            [message1 saveInBackground];

            [self.tableView reloadData];

        }

    }];

Yes I tried and gives me an error like this: *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1582 (lldb) .

But without this line [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; everithing is OK but the row don't disappear immediate. I don't know what to do, maybe the problem is from another part........

there should be more information about the error. scrol down in the console and see what it else it says

Dear Preston I did a screen shot with error. http://tinypic.com/r/102wleg/8

post your other tableView delegate and data source methods. you most likely have an issue in those methods thats causing this problem

I put all my project into a ZIP file. http://tinyurl.com/q7bfh5l

I will be very thankful if you will take a look at my code. Thanks in advance!!!!

nothing really looks incorrect with your code. maybe try a different animation style such as automatic:

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

I spend 2 days and I don't know what's the problem, I asked in stackoverflow and nobody knows what's the problem.......

Could you upload your code to Github so I can see the whole app?