简体   繁体   English

调用reloadData后NSArray为空

[英]NSArray Empty After Calling reloadData

I have a class that gets data from a repository and stuffs it into an NSArray: 我有一个从存储库获取数据并将其填充到NSArray中的类:

EpisodeRepository Class EpisodeRepository类

-(NSMutableArray *)getEpisodes { -(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;

}

In my view controller, I keep a reference to the repo and the array of episodes, like so: 在我的视图控制器中,我保持对仓库和情节数组的引用,如下所示:

@interface VideoController : UITableViewController

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

In the implementation, what I want to happen is that every the view appears, refetch the data from the web services(in the EpisodeRepository) and reload the table view with the updated episodes. 在实现中,我要发生的是每个视图都出现,从Web服务(在EpisodeRepository中)重新获取数据,然后用更新的情节重新加载表视图。 This works great the first time the tableview is populated. 第一次填充tableview时,这种方法效果很好。 The problem is that when I call, cellForRowAtIndexPath, the episodes array is completely blank. 问题是,当我调用cellForRowAtIndexPath时,情节数组完全空白。 It's not null, but there is no data whatsoever in it. 它不是空值,但是里面没有任何数据。 The weird thing is that all the other tableView delegates realize that there is data in the array and act accordingly. 奇怪的是,所有其他tableView委托都意识到数组中存在数据并采取相应的措施。 What is special about cellForRowAtIndexPath and why would it possibly delete my entries in the array? cellForRowAtIndexPath有什么特别之处,为什么它可能删除数组中的条目?

Here is the controller implementation: 这是控制器的实现:

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

I found the solution! 我找到了解决方案! It turns out I had code in my App Delegate under the willSelectViewController for the tab bar that was allocating the array outside of the view controller. 事实证明,我的应用程序委托中的代码在willSelectViewController下的选项卡栏下,该标签栏将数组分配到视图控制器之外。 Lesson learned: don't do this! 经验教训:不要这样做!

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

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