簡體   English   中英

刪除行時崩潰-未更新部分中的不一致

[英]Crash when deleting row - Inconsistency in section not being updated

當我使用commitEditingStyle從UITableView刪除一行時,我的應用程序崩潰並顯示以下錯誤消息。 奇怪的是,我正在刪除第3節。根據消息顯示的不一致來自第4節。

*-[UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-1262.60.3 / UITableView.m中的斷言失敗:920 2010-11-22 19:56:44.789 bCab [23049:207] *由於未捕獲的異常而終止應用程序'NSInternalInconsistencyException',原因:'無效更新:第4節中的行數無效。更新(1)之后現有節中包含的行數必須等於更新前該節中包含的行數(0 ),加上或減去從該部分插入或刪除的行數(已插入0,已刪除0)。

在數據源中,我根據第3節中的行數更新第4節。當從第3節中刪除一行時,第4節中的行數從0變為1。這似乎引起了問題。 有沒有辦法避免這種情況?

任何指針將不勝感激。 謝謝。

更新

numberOfSectionsInTableView- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 6;
}

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
bCabAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    if (section == 0) {        // First name, last name
        return 2;
    }
    else if (section == 1) {   // Password
        return 2;   
    }
    else if (section == 2) {   // Mobile, DOB , Gender
        return 3;
    }
    else if (section == 3) {    // credit cards
        return [creditCards count]; 
    }
    else if (section == 4) {    // Add credit card 
        if ([creditCards count] >= 3) {
            return 0;   
        }
        else {
            return 1;   
        }
    }
    else if (section == 5) {
        return 0;   
    }

    return 0;

}

commitEditingStyle

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (indexPath.section == 3) {
        // Delete the row from the data source  
        NSLog(@"%d %d", indexPath.section, indexPath.row);
        [creditCards removeObjectAtIndex:indexPath.row];

        // Delete from backend

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

        //[tableView reloadData];
        }
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }  

}

我嘗試過使用[tableView reloadData]和不使用它都具有相同的結果。

感謝您的幫助!

在數據源中,我根據第3節中的行數更新第4節。當從第3節中刪除一行時,第4節中的行數從0變為1。這似乎引起了問題。 有沒有辦法避免這種情況?

使用deleteRowsAtIndexPaths:withAnimation您保證數據源將只刪除具有指定索引路徑的行。 在您的情況下,您還將在表中插入一行,這意味着數據源的狀態不是表視圖所期望的。

當刪除第3節中的一行(還涉及在第4節中插入一行)時,您必須執行以下操作:

[self.tableView beginUpdates];
[self.tableView [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPath:[NSArray arrayWithObject:indexPathForInsertedRow] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

刪除單元格之前,是否要刪除creditCards中的相應對象?

刪除第3節中的行后,如何在第4節中添加新行?
這不是一個好的解決方案,但是當刪除行時,您可以嘗試制作一個reloadData。

暫無
暫無

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

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