(已解决)在调用numberOfRowsInSection时,TableView初始化程序中设置的variables会被遗忘

我现在在我的第一篇文章中已经提到过这个问题,但是我不能解决这个问题。 而且我有点不习惯再次问这个问题,但是我现在正在写我的学士论文(而且我是XCode和Objective-C的新手),我真的需要完成这个任务。 高兴的帮助我,如果有一些信息丢失,请致电,我很乐意与您沟通。 : – /

我使用XCode 4.5与ARC来创build一个UITableViewController类,我想用NSDictionary初始化它。 在它的初始化器中,创build的[resourcesAsDictionary count]返回一些正确的值,但后来当我按下我的主视图中的button,并根据NSDictionary的条目数build立我初始化类,NSDictionary总是空的,[resourcesAsDictionary计数]返回0.我认为这必须与ARC? 如果我能把它关掉,我可以保留这个variables? 但是有一个方法可以得到这个WITH ARC?)相关的代码:

编辑:我已经根据我收到的帮助改变了代码。 编辑2:因为我能够缩小问题,这是有点混乱,远远要读很多我创build了一个新的话题: ViewController类创build新的实例时创build视图,但它应该使用现有的编辑3:问题在已经链接的线程中解决了,这个也是。

ViewController.h:

#import <UIKit/UIKit.h> #import <Foundation/NSJSONSerialization.h> #import <Foundation/NSXMLParser.h> #import "ResourcesTableViewController.h" @interface ViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource> { // Note: this <UITableViewDelegate, UITableViewDataSource> is NOT related to the TableViewController. My ViewController has another TableView. NSDictionary *parsedResponseAsDictionary; ResourcesTableViewController *resourceTableViewController; ... } ... 

在ViewController.m中的某处:

 #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; parsedResponseAsDictionary = [[NSDictionary alloc] init]; } ... - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_bodyData { parsedResponseAsDictionary = [NSJSONSerialization JSONObjectWithData:bodyData options:NSJSONWritingPrettyPrinted error:&err]; ... // putting found keys as entries in an array foundResources if ([foundResources count] > 0) { resourceTableViewController = [[ResourcesTableViewController alloc] initWithStyle:UITableViewStylePlain]; [resourceTableViewController setResourcesAsDictionary:parsedResponseAsDictionary]; NSLog(@"Delivered %i entries.",[[resourceTableViewController resourcesAsDictionary] count]); // TableView can be built: enable "Show Resources" button [_showResourcesButton setAlpha:1]; [_showResourcesButton setEnabled:YES]; } } ... // Output: // REST Analyzer[1259:c07] Delivered 8 entries. // REST Analyzer[1259:c07] viewWillAppear: (null) // REST Analyzer[1259:c07] Now: 0 entries. 

ResourcesTableViewController.h:

 #import <UIKit/UIKit.h> @interface ResourcesTableViewController : UITableViewController { } @property (nonatomic, strong) NSDictionary *resourcesAsDictionary; @end 

ResourcesTableViewController.m:

 #import "ResourcesTableViewController.h" @implementation ResourcesTableViewController // - (id)initWithDictionary:(NSDictionary*)dic { // if (self = [super init]) // resourcesAsDictionary = [[NSDictionary alloc] initWithDictionary:dic]; // NSLog(@"resource table created with %i entries.",[resourcesAsDictionary count]); // return self; // } - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; return self; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewWillAppear:(BOOL)animated { NSLog(@"viewWillAppear: %@", self.resourcesAsDictionary); // (null) is returned here. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"Now: %i entries.",[self.resourcesAsDictionary count]); return [self.resourcesAsDictionary count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; } cell.textLabel.text = @"Cell"; cell.detailTextLabel.text = @"Description"; return cell; } 

我会以几种方式改变这个…

删除iVar for resourcesAsDictionary并将其设置为属性。

 @property (nonatomic, strong) NSDictionary *resourcesAsDictionary; 

然后,我会改变初始化通过删除您的init方法,并使用此属性来代替。

在viewController,推到这个控制器(即你在哪里调用initWithDictionary)…

而不是initWithDictionary有…

 ResourcesTableViewController *tvc = [[ResourcesTableViewController alloc] initWithStyle:<whicheverstyleyouneed>]; 

然后设置属性…

 tvc.resourcesAsDictionary = mySourceDictionary; 

然后按下TVC …

 [self.navigationController pushViewController:tic animated:YES]; 

…或者你正在把它推到屏幕上。

最后,您需要将您的numberOfRows …代码更改为…

 return [self.resourcesAsDictionary count]; 

这将是我的方法,这意味着你不必重写UITableViewController类的init。

通过拥有强大的财产,你是确保它将留在拥有的对象的生活周围。 通过不覆盖init,确保你不会错过任何在initWithStyle中调用的代码。

如果这不起作用,那么我只能认为,首先创build字典的方式有些不对。

在 – (void)viewWillAppear …函数告诉我的输出…

 NSLog(@"%@", self.resourcesAsDictionary);