簡體   English   中英

UITableView和numberOfRowsInSection崩潰

[英]UITableView and numberOfRowsInSection crash

嘗試從UITableView刪除行時遇到問題:UITableView從NSMutableArray獲取行數:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [alertsArray count];
}

我以這種方式將對象添加到NSMutableArray中:

- (void)saveButton:(id)sender {
[alertsArray addObject:
[self.tableView reloadData];
}

使用此代碼,我可以刪除行:

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

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source.
    [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [alertsArray removeObjectAtIndex:indexPath.row];
    [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.
}   
}

由於某種原因,當嘗試刪除行時,應用程序崩潰。

謝謝!

你應該做

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source.
    [alertsArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} 

也就是說,首先更正您的模型, 然后調用deleteRowsAtIndexPaths:

您將在控制台窗口BTW中找到適當的錯誤消息。

同樣,當您也沒有更改其他內容時,通常無需在此處使用reloadData

暫無
暫無

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

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