繁体   English   中英

调用reloadData后NSArray为空

[英]NSArray Empty After Calling reloadData

我有一个从存储库获取数据并将其填充到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;

}

在我的视图控制器中,我保持对仓库和情节数组的引用,如下所示:

@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???
}

我找到了解决方案! 事实证明,我的应用程序委托中的代码在willSelectViewController下的选项卡栏下,该标签栏将数组分配到视图控制器之外。 经验教训:不要这样做!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM