簡體   English   中英

嘗試在UITableView中插入新行時,iOS應用程序崩潰

[英]iOS application crashes when trying to insert new row in UITableView

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];

這段代碼使我的iOS應用程序崩潰,並返回以下錯誤:

無效的更新:第0節中的行數無效。更新(0)之后,現有節中包含的行數必須等於更新(0)前該節中包含的行數,再加上或減去該數字該部分插入或刪除的行數(已插入1個,已刪除0個)。

有人可以告訴我為什么嗎? 怎么說更新后的行數為零,而同時說1插入 ,0刪除呢?

更新

當我在運行時收到推送通知時,上面的代碼就會觸發。 發生這種情況時,委托將接收到的數據添加到存儲在.plist文件(表視圖用於填充其單元格)中的字典中,然后調用RootViewController中的自定義方法,該視圖位於表視圖中。 此方法執行上面的代碼,然后崩潰。 然而,當我登錄[theDictionary count]- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section的數量遞增,就像它應該崩潰之前

另外值得注意的是,當我登錄self.tableview時,當RootViewController在啟動時加載時以及在接收到推送通知時,會從RootViewController接收不同的“內存地址”。 也許這與它有關?

這是我的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法:

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

和錯誤的圖片:

錯誤

每當您對表視圖的數據源進行任何更改時, numberOfRowsInSection方法都應返回正確的行數。

例如,如果表中的總行數為5,並且您打算再增加一行,則numberOfRowsInSection方法應返回6。

通過將數據存儲在arraydictionary可以輕松實現。 如果要添加或刪除行,只需更新數組或字典。 只需從numberOfRowsInSection方法返回[array count][dictionary count]

暫無
暫無

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

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