2015-06-25 6 views
0

Я разрабатываю приложение IOS. Удалите строку в таблице и загрузке данных в основных данных в виде таблицы. Нажмите кнопку «Удалить». Причина Причина: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (18) must be equal to the number of rows contained in that section before the update (18), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)...Удалить строку из таблицы вид

_fetchedobj является NSArray для извлечения данных из основных данных

//Code is 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return _fetchedobj.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 
    data = [_fetchedobj objectAtIndex:indexPath.row]; 
    return cell; 
} 

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [tableView beginUpdates]; 
     NSManagedObject *managedObject = [_fetchedobj objectAtIndex:indexPath.row]; 
     [self.managedObjectContext deleteObject:managedObject]; 
     [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [myTable endUpdates]; 
    } 
} 
+0

Что делает вашу реализацию '-tableView: cellForRowAtIndexPath:' выглядеть ? – jperl

+0

Если вы не выполняете '[_fetchedobj removeAtIndex: indexPath.row]' в delete? В противном случае вы не обновили модель данных, используемую для определения количества строк. –

ответ

1

Вы не забыли удалить этот объект из массива данных?

попробуйте повторно заполнить _fetchedobj, потому что вы должны настроить свою модель после удаления. Более подробно, читайте https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

Попробуйте

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [tableView beginUpdates]; 
     NSManagedObject *managedObject = [_fetchedobj objectAtIndex:indexPath.row]; 
     [self.managedObjectContext deleteObject:managedObject]; 
     // insert this line to your code 

     NSMutableArray *newA = [_fetchedobj mutableCopy ]; 
     [newA removeObjectAtIndex: indexPath.row]; 
     _fetchedobj = newA; 

     [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [myTable endUpdates]; 
    } 
} 
0

Попробуйте использовать эту строку для удаления строки из представления таблицы

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
Смежные вопросы