2015-04-08 3 views
1

Код для моего UITableView не краснеет, что может только meen мой массив не заселен. Я использую core-database, и, как я вижу из печати, я получаю, что это показывает, что у моей БД есть данные. Он просто не отображается. Вот мой код:NSMutableArray не заселен

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:nil]; 

    [self reloadArray]; 

} 
-(void) reloadArray 
{ 

    AppDelegate *delegate = [UIApplication sharedApplication].delegate; 
    NSManagedObjectContext *context = [delegate managedObjectContext]; 

    NSFetchRequest *fetch = [[NSFetchRequest alloc]init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context]; 
    [fetch setEntity:entity]; 
    NSManagedObject *object = nil; 
    NSError *error; 
    NSArray *result = [context executeFetchRequest:fetch error:&error]; 

    for (int i = 0; i < [result count]; i ++) { 
     object = [result objectAtIndex:i]; 
     [_projectArray addObject:[object valueForKey:@"name"]]; 
    } 



} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    [self reloadArray]; 
    return 1; 
} 

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

    return [_projectArray count]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellID = @"TableCell"; 
    NSManagedObject *object = [self.projectArray objectAtIndex:indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 

    tableView.delegate = self; 
    tableView.dataSource = self; 
    cell.textLabel.text = [_projectArray objectAtIndex: indexPath.row]; 
    return cell; 
} 
+1

Прежде всего установите точку останова после выполнения запроса на выборку и проверьте, что говорит об ошибке. – user2260054

+1

Зачем вам нужно [self reloadArray]; в numberOfSectionsInTableView? – user2260054

+0

При настройке точки останова она показывает, что Error = nil, и она показывает, что в таблице видны 4 строки, 4 элемента в Entity @ «Project» и 4 элемента в массиве. –

ответ

2

Вы устанавливаете свой delegate и dataSource в tableView: cellForRowAtIndexPath:. Они должны быть установлены при первоначальной настройке вида таблицы, не, когда вызывается метод delegate (потому что этот метод никогда не будет вызван).

Чтобы повторно итерацию, установить delegate и dataSource свойства в viewWillAppear:animated: или viewDidLoad:

- (void)viewDidLoad { 

    tableView.delegate = self; 
    tableView.dataSource = self; 
} 

В противном случае установить их перед вызовом reloadData приведет ни в одном из ваших delegate или dataSource методов называют.

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