Hannah Vuppula
Hannah Vuppula

Reputation: 21

App Crashes When the Swipe to Delete feature is used Twice on the same row

I have one UITableView in this View Controller. When I run the app, and use the swipe to delete function (say on row 3), it deletes the row perfectly the first time. If I add new data to the same row (row 3), and perform the swipe to delete action again, the app crashes, and gives me a EXC_BAD_ACCESS error. This is a part of my code:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if editingStyle == UITableViewCellEditingStyle.Delete {
        toDoList.removeObjectAtIndex(indexPath.row)
        feedTimes = feedTimes - 1
        ft.text = "\(toDoList.count)"
        tableView.reloadData()
    }
}

Can anyone explain why this may be happening? Thanks for your time.

Upvotes: 2

Views: 373

Answers (2)

dimpiax
dimpiax

Reputation: 12677

Seems you have incorrect values for update. Check them repeatedly. By the way, in this case, instead of reloadData use:

performBatchUpdates {
    deleteRowsAtIndexPaths
    // or
    deleteSections
}

relatively to your table structure

Upvotes: 1

DanielS
DanielS

Reputation: 181

Apple in Inserting and Deleting Rows and Sections document proposes:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        SimpleEditableListAppDelegate *controller = (SimpleEditableListAppDelegate *)[[UIApplication sharedApplication] delegate];
        [controller removeObjectFromListAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

I used to use this approach successfully.

Upvotes: 1

Related Questions