簡體   English   中英

如何在拆分視圖控制器中刪除TableView行

[英]How to delete tableview row in split view controller

我試圖刪除拆分視圖控制器中顯示的tableview行。 當我向左滑動並單擊“刪除”時,我的應用程序崩潰,並且出現錯誤:

由於未捕獲的異常'NSInternalInconsistencyException'而終止應用程序,原因:'無效的更新:第0節中的行數無效。更新(44)后現有節中包含的行數必須等於該行中包含的行數更新前的部分(44),加上或減去從該部分插入或刪除的行數(已插入0,已刪除1),加上或減去該部分移入或移出的行數(移入0,移入0)出)。”

我確實設法使re-arrange選項適用於表格視圖,而沒有任何錯誤。 對於刪除行功能,我執行了以下操作:

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_objects removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
    withRowAnimation:UITableViewRowAnimationAutomatic];   
}

但是肯定有一些我想念的東西。 也許是一個簡單的更新tableview。 我想我嘗試過,但是沒有運氣。

我知道很難用我的項目中的一種方法來確定方法。 您可以在這里查看我的整個項目:

https://github.com/mithunan/presidents-splitView

感謝您的幫助。

編輯 - -

這是numberOfRowsInSection方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.presidents count];
}

這種方法也可能會幫助...

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    id object = [self.presidents objectAtIndex:fromIndexPath.row];
    [_objects removeObjectAtIndex:fromIndexPath.row];
    [_objects insertObject:object atIndex:toIndexPath.row];
}

謝謝

編輯2 -------------

根據rmaddy提供的信息,我更改了commitEditingStyle ...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.presidents removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

但是現在我收到一個構建錯誤:NSArray的無可見@interface聲明選擇器'removeObjectAtIndex:'

抱歉,如果我要很多,請您指導我。

每當更改表視圖模型時,添加beginUpdatesendUpdates方法調用始終是一個好主意:

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    [_objects removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
}

beginUpdates方法的文檔中:

開始一系列方法調用,這些方法插入,刪除或選擇接收器的行和部分。 如果希望后續的插入,刪除和選擇操作(例如cellForRowAtIndexPath:和indexPathsForVisibleRows)同時動畫,請調用此方法。 這組方法必須以調用endUpdates作為結束。 這些方法對可以嵌套。 如果未在此塊內進行插入,刪除和選擇調用,則表屬性(例如行數)可能會變得無效。 您不應在該組內調用reloadData; 如果在組中調用此方法,則需要自己執行任何動畫。

暫無
暫無

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

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