【发布时间】:2012-11-14 07:27:26
【问题描述】:
我有一个从存储库中获取数据并将其填充到 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???
}
【问题讨论】:
-
发布 getDataFromAction 实现。是异步的吗?
标签: iphone uitableview nsarray automatic-ref-counting