当我尝试从此效率上删除对象/行时,apllication用日志崩溃:
nsinternalInconSistencyException',原因:'无效更新:无效 第0节中的行数
编辑:
代码中存在更改,现在插入了新对象,并且实际删除了删除的对象,但是只有在崩溃并重新启动后才删除……因此,删除仍然使用相同的日志失败,无效的inttade ...新代码:
@interface TableViewController ()
@property NSMutableArray *users;
@property NSArray *detailList;
@property (strong) NSMutableArray *locations;
@end
@implementation TableViewController
- (IBAction)delete:(UIBarButtonItem *)sender {
self.tableView.editing = !self.tableView.editing;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.detailList=@[@"Your Favourite Spots",@"Our suggestion"];
}
-(void)viewDidAppear:(BOOL)animated{
_locations = [[Spot spotType:@"users"]mutableCopy];
_users=[[Spot spotWithType:@"users"]mutableCopy];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.detailList objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *places=[self containerWithSection:section];
return places.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSArray *places=[self containerWithSection:indexPath.section];
Spot *spot = [places objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", spot.name]];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"detail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSArray *places = [self containerWithSection:indexPath.section];
DetailViewController * destination = segue.destinationViewController;
destination.spot = [places objectAtIndex:indexPath.row];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSPersistentContainer *persistenceContainer = [AppDelegate sharedDelegate].persistentContainer;
NSManagedObjectContext *context = persistenceContainer.viewContext;
NSMutableArray *places = [self containerWithSection:indexPath.section];
Spot *spot = [places objectAtIndex:indexPath.row];
[context deleteObject: spot];
NSError *error;
[context save:&error];
if (!error) {
NSLog(@"%@", error);
}
[places removeObject:spot];
[self updateItemInSection:indexPath.section];
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
[tableView reloadData];
}
#pragma mark - Aux methods
- (void)updateItemInSection:(NSUInteger)section{
switch (section) {
case 0:{
_locations = [[Spot allSpots]mutableCopy];
}
break;
case 1:{
_users = [[Spot spotType:@"users"]mutableCopy];
}
break;
}
}
#pragma mark - Aux methods
- (NSMutableArray*)containerWithSection:(NSUInteger)section {
NSMutableArray *thisContainer;
switch (section) {
case 0:
thisContainer = self.locations;
break;
case 1:
thisContainer = self.users;
break;
}
return thisContainer;
}
@end
删除/插入数据对象后,您需要首先更新数据阵列,然后才能要求表recroddata。这就是崩溃的原因,也没有使用新元素进行更新。
如果您考虑这种推理&尝试一次对您的代码进行必要的更改。
请记住,在任何更改时,插入,删除数据=>首先,更新数据数组=>然后,[table reloadData]
因此,现在为了处理单元格的删除,我们将添加a new 函数updateItemInSection:
负责执行表数据更新&将从tableView:commitEditingStyle:forRowAtIndexPath:
调用
它的外观:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSPersistentContainer *persistenceContainer = [AppDelegate sharedDelegate].persistentContainer;
NSManagedObjectContext *context = persistenceContainer.viewContext;
NSManagedObject *spot = [_lisbonSpots objectAtIndex:indexPath.section];
[context deleteObject: spot];
NSError *error;
[context save:&error];
if (!error) {
NSLog(@"%@", error);
}
[_lisbonSpots removeObject:spot];
// new function invocation here
[self updateItemInSection:indexPath.section];
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)updateItemInSection:(NSUInteger)section{
switch (section) {
case 0:{
_bar = [Spot spotType:@"bar"];
}
break;
case 1:{
_restaurant = [Spot spotType:@"restaurant"];
}
break;
case 2:{
_club = [Spot spotType:@"club"];
}
break;
case 3:{
_others = [Spot spotType:@"others"];
}
break;
}
}
- (NSArray*)containerWithSection:(NSUInteger)section {
NSArray *thisContainer;
switch (section) {
case 0:
thisContainer = self.bar;
break;
case 1:
thisContainer = self.restaurant;
break;
case 2:
thisContainer = self.club;
break;
case 3:
thisContainer = self.others;
break;
}
return thisContainer;
}
问题是名字令人困惑...与编辑的代码,解决方案是:
- (void)updateItemInSection:(NSUInteger)section{
switch (section) {
case 0:{
_locations = [[Spot spotWithType:@"users"]mutableCopy];
}
break;
case 1:{
_users = [[Spot spotType:@"users"]mutableCopy];
}
break;
}
}
,但是没有@ystack的帮助