调用reloadData后NSArray为空

时间:2012-11-14 07:27:27

标签: iphone uitableview nsarray automatic-ref-counting

我有一个从存储库获取数据并将其填充到NSArray中的类:

EpisodeRepository类

- (NSMutableArray *)getEpisodes {

NSMutableArray *episodes = [NSMutableArray array];

NSData *data = [self getDataFromAction:kGetEpisodesAction];
NSError *error = nil;
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

for (NSDictionary *d in json) {      
    Episode *episode = [self buildEpisodeFromJSONDictionary:d];
    [episodes addObject:episode];
}

return episodes;

}

在我的视图控制器中,我保留了对repo和剧集数组的引用,如下所示:

@interface VideoController : UITableViewController

@property (nonatomic, strong) NSArray *episodes;
@property (nonatomic, strong) EpisodeRepository *episodeRepo;

在实现中,我想要发生的是每个视图出现,从Web服务(在EpisodeRepository中)重新获取数据并使用更新的剧集重新加载表视图。这在第一次填充tableview时效果很好。问题是当我调用cellForRowAtIndexPath时,剧集数组完全是空白的。它不是空的,但它没有任何数据。奇怪的是,所有其他tableView委托都意识到数组中有数据并采取相应的行动。有关cellForRowAtIndexPath的特别之处以及为什么它可能会删除数组中的条目?

这是控制器实现:

-(id) initWithCoder:(NSCoder *)aDecoder {

self = [super initWithCoder:aDecoder];

if(self) {
    episodeRepo = [[EpisodeRepository alloc] init];
}

return self;

}

-(void)viewWillAppear:(BOOL)animated
{
    [self loadContent];
    [self.tableView reloadData];
    [super viewWillAppear:animated];
}

-(void) loadContent {    
    self.episodes = [self.episodeRepo getEpisodes];
    self.seasons = [self getSeasons:self.episodes];
    [self setUILoaded];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //episodes is an empty array here???
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!事实证明,我在我的App Delegate中的代码位于willSelectViewController下,用于在视图控制器之外分配数组的选项卡栏。获得的经验:不要这样做!