簡體   English   中英

輕按即可刪除崩潰提示

[英]Swipe to delete crashes when tapped

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        PFObject *object = [_tdlArray objectAtIndex:(_tdlArray.count - indexPath.row -1)];
        [object deleteInBackground];

        //found the code for removing a row.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView reloadData];
        [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!succeeded){

                [tableView reloadData];

            }

        }];

    }

}

我能夠成功刪除數據,但是每次我點擊刪除按鈕時,我的應用都會崩潰。 我認為這與[NSArray arrayWithObject:indexPath]

這些是錯誤消息

Assertion failure in -[UITableView _endCellAnimationsWithContext:]
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 (3) must be equal to the number of rows contained in that section before the update (3), 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).'

您要刪除對象,然后重新加載數據。 不要異步調度要刪除的對象,然后告訴tableview您要刪除行,因為該對象可能還沒有刪除,因此會出現錯誤。 刪除對象后,使用回調塊更新表視圖,這樣可以確保對象已刪除。 同樣,如果您的數據的本地存儲未綁定到服務器上的數據,則也需要從那里刪除該對象。

   - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //not sure how you're calculating the index here
            PFObject *object = [_tdlArray objectAtIndex:(_tdlArray.count - indexPath.row -1)];
            NSMutableArray *mutArray = [_tdlArray mutableCopy];
            [mutArray removeObject:object];
            _tdlArray = [NSArray arrayWithArray:mutArray];
            [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!succeeded){
                    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                    [tableView reloadData];

                }

            }];

        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM