调用numberOfRowsInSection时,忘记了TableView初始值设定项中的变量集 [英] Variable set in the TableView initializer is forgotten when numberOfRowsInSection is called

查看:78
本文介绍了调用numberOfRowsInSection时,忘记了TableView初始值设定项中的变量集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有ARC的Xcode 4.5创建一个UITableViewController类,我想用NSDictionary初始化它。在它的初始化程序中,创建的[resourcesAsDictionary计数]返回一些正确的值,但稍后当我在主视图中按下一个按钮时,该表是根据我初始化类的NSDictionary的条目数构建的,NSDictionary永远是空的,[resourcesAsDictionary计数]返回0.
我认为这与ARC有关吗?如果我把它关掉,我可以保留那个变量吗?但必须有一种方法可以获得这个WITH ARC?)
相关代码:

I'm using Xcode 4.5 with ARC to create a UITableViewController class and I want to initialize it with a NSDictionary. In its initializer the created [resourcesAsDictionary count] is returning some proper value but later on when I'm pressing a button in my main view and the table is built according to the number of entries of the NSDictionary I initialized the class with, the NSDictionary is always empty and [resourcesAsDictionary count] returns 0. I assume this has to do with ARC? If I would turn that off, I could retain that variable? But there has to be a way do get this WITH ARC?) The relevant code:

编辑:我已根据收到的帮助更改了代码。$
edit2:由于我能够缩小问题范围并且这有点令人困惑并且阅读起来很多,我创建了一个新主题: ViewController类在创建视图时创建新实例,但它应该使用现有的实例

编辑3:在已经链接的线程中解决了问题,这就是这个问题。

edit: I already changed the code according to the help I received.
edit2: Since I was able to narrow the problem down and this was a bit confusing and far to much to read in I created a new topic: ViewController class creates new instance when creating view, but it should use an existing one
edit 3: Problem solved in the already linked thread, so is this one.

ViewController.h:

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:

Somewhere in 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:

ResourcesTableViewController.h:

#import <UIKit/UIKit.h>

@interface ResourcesTableViewController : UITableViewController {
}

@property (nonatomic, strong) NSDictionary *resourcesAsDictionary;

@end

ResourcesTableViewController.m:

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;
// }

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"viewWillAppear: %@", self.resourcesAsDictionary);
    // (null) is returned here.
}

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


推荐答案

我会在几种方式...

I would change this in a couple of ways...

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

Remove the iVar for resourcesAsDictionary and set it as a property instead.

@property (nonatomic, strong) NSDictionary *resourcesAsDictionary;

然后我将通过删除init方法并使用此属性来更改初始化。

Then I'd change the initialisation by removing your init method and using this property instead.

在推送到此控制器的viewController中(即调用initWithDictionary的地方)...

In the viewController that pushes on to this controller (i.e. where you call the initWithDictionary)...

而不是initWithDictionary拥有...

Instead of initWithDictionary have...

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

然后设置属性...

tvc.resourcesAsDictionary = mySourceDictionary;

然后按TVC ...

Then push the TVC...

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

...或者你将它推到屏幕上。

...or however you are pushing this to the screen.

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

Finally you'll need to change your numberOfRows... code to...

return [self.resourcesAsDictionary count];

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

The would be my approach and it means you don't have to override the init of the UITableViewController class.

通过拥有强大的财产,您确保它将在拥有对象的生命周期中保持不变。通过不覆盖init,你可以确保你没有错过任何在initWithStyle中调用的代码...

By having the strong property you are ensuring it will stay around for the life of the owning object. And by not overriding the init you ensure that you're not missing out on any code that gets called in initWithStyle...

如果这不起作用那么我可以只是认为你首先创建字典的方式不对。

If this doesn't work then I can only think that there is something not right with the way you are creating the dictionary in the first place.

在 - (void)viewWillAppear ...函数告诉我输出......

In the - (void)viewWillAppear... function tell me the output of...

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

这篇关于调用numberOfRowsInSection时,忘记了TableView初始值设定项中的变量集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆