1

У меня есть главный столПросмотрСохраните, нажмите кнопку self.navigation будет нажать addItem viewController, нажмите save self.navigation с поп добавить VC, то мы вернемся к основному, Я успешно добавить и сохранить, а также получать, но когда дело доходит до извлечения количество ячеек, число возвращает 0, Heres методНе удается получить количество ячеек в таблицеView

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

id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]  objectAtIndex:section]; 
NSLog(@"No. of cells determined: %lu", (unsigned long)[secInfo numberOfObjects]); 
return[secInfo numberOfObjects]; 

} 

NSLog дает мне 0, что проблема, это дало мне головную боль.

Вот выборки запроса:

- (NSFetchedResultsController*) fetchedResultsController { 

// Fetch Request 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Goal" 
              inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" 
                   ascending:NO]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 




// setting _fethcedResultsController 
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                   managedObjectContext:self.managedObjectContext 
                    sectionNameKeyPath:nil 
                      cacheName:nil]; 




// setting _fetchedResultsController to self 
_fetchedResultsController.delegate = self; // for the tableview updating thing 



// Thats it 
return _fetchedResultsController; 

} 

Пожалуйста, обратите внимание, что при запуске чек на предметы, это не ноль:

// fetching all items to check how many of them exists 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
[fetchRequest setEntity: [NSEntityDescription entityForName:@"Goal" 
            inManagedObjectContext:self.managedObjectContext]]; 
NSManagedObjectContext *moc = self.managedObjectContext; 

NSError *fetchError = nil; 
NSArray *goals = [moc executeFetchRequest:fetchRequest error:&fetchError]; 

NSLog(@"No. of goals: %lu", (unsigned long)[goals count]); 
// end of check for items 
+2

Вы используете основные данные? Покажите, пожалуйста, ваш 'NSFetchRequest'. –

+0

done @ValentinShamardin – CodeLover

+0

использовать точку останова или распечатать то, что содержится в NSError * fetchError. Может возникнуть некоторая ошибка в fetchrequest –

ответ

1

Я забыл поставить этот код строки:

if (_fetchedResultsController != nil) { 
    return _fetchedResultsController; 
} 

по методу

- (NSFetchedResultsController*) fetchedResultsController; 

так, прежде чем я это сделал, приложение всегда создает новый fetchedResultsController и переписывает старый.

Смежные вопросы