2015-01-09 2 views
1

У меня есть представление таблицы и настраиваемая ячейка, которые загружаются и настраиваются, но проблема в том, что данные не загружаются, если я не поворачиваю устройство. В портретном режиме, когда я впервые запускаю его, там ничего нет, как только я вращаю устройство в любом случае, все данные загружаются и работают отлично. Какие-либо предложения?iOS table view только загружает данные на вращение устройства

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = @"Hello"; return cell; 
} 

Загрузка данных -

PFQuery *query = [PFQuery queryWithClassName:@"Post"]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     NSLog(@"%@", objects); 
     _postsArray = [[NSArray alloc] initWithArray:objects]; 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error loading the posts. Please try again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 
}]; 
[self.tableView reloadData]; 

ответ

2

Ваша проблема заключается в том, что вы загружаете данные асинхронно и не вызывая reloadData когда загрузка завершена. Вы вызываете этот метод, но вне блока, поэтому он будет выполнен немедленно, прежде чем загрузка будет завершена.

Ваш метод загрузки данных должно быть -

PFQuery *query = [PFQuery queryWithClassName:@"Post"]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     NSLog(@"%@", objects); 
     _postsArray = [[NSArray alloc] initWithArray:objects]; 
     dispatch_async(dispatch_get_main_queue(),^{ 
      [self.tableView reloadData]; 
     }); 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error loading the posts. Please try again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     dispatch_async(dispatch_get_main_queue(),^{ 
      [alert show]; 
     }); 
    } 
}]; 

Обратите внимание, что операции, которые влияют на пользовательский интерфейс должны быть выполнены на главной очереди.

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