Я немного растерялся из-за простого действия по удалению строки из табличного представления.У меня есть база данных sqlite с таблицей SUBJECT, и я хочу удалить запись для этой таблицы, удалить элемент из массива и удалить запись в табличном представлении.Я не знаю, какая последовательность лучше, и я продолжаю получать ошибки.
Вот код:
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
//Get the object to delete from the array.
Subject *sub = [self.subjects objectAtIndex:indexPath.row];
// check if there are any quoteMaps for this subject and if so do not do it
NSArray *qm = [appDelegate getQuoteMapsFromSubId:sub.subject_id];
if (qm.count==0){
//Delete the object from the table.
[self.subjects removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[sub deleteSubject];
} else {
//DON'T DO IT BUT GIVE A WARNING INSTEAD
}
}
}
Я получаю следующую ошибку на строке self.subjects removeObject ....:
-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6b2a8f0
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6b2a8f0'
Сообщение об ошибке говоритмой self.subjects является NSArray, но мой заголовок назначает его как NSMutableArray:
@interface SubjectViewController : UITableViewController <UIAlertViewDelegate> {
NSMutableArray *subjects;
}
@property (nonatomic, retain) NSMutableArray *subjects;
Почему он меняется на NSArray ???
Я проверил и обнаружил, что родительский ViewController назначаетследующее:
NSPredicate *catFilter = [NSPredicate predicateWithFormat:@"category_title == %@", cat.category_title];
NSArray *subs = [appDelegate.subjects filteredArrayUsingPredicate:catFilter];
subViewController.subjects = (NSMutableArray *) subs;
Похоже, проблема в этом.
Я получаю следующую ошибку, если закомментирую self.subjects removeObject и попытаюсь запустить следующую строку «tv deleteRowsAtIndexPaths ...»:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Invalid update: invalid number of rows in section 0. The number of rows
contained in an existing section after the update (1) must be equal to the number
of rows contained in that section before the update (1), 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).'
Пока это сужаетсявплоть до проблемы в предыдущем представлении контроллера, где я использую NSArray из NSPredicate, потому что я не мог заставить NSPredicate работать с NSMutableArray.Так что теперь это проблема, которая пытается решить.
Или преобразование / назначение NSArray в качестве NSMutableArray ...