-2

Xcode 4.5 と ARC を使用して UITableViewController クラスを作成していますが、NSDictionary で初期化したいと考えています。そのイニシャライザでは、作成された [resourcesAsDictionary count] は適切な値を返しますが、後でメイン ビューでボタンを押すと、クラスを初期化した NSDictionary のエントリ数に従ってテーブルが構築されます。は常に空で、[resourcesAsDictionary count] は 0 を返します。これは ARC と関係があると思いますか? それをオフにすると、その変数を保持できますか? しかし、これをWITH ARCで取得する方法が必要ですか?) 関連するコード:

編集:受け取ったヘルプに従って、コードをすでに変更しました。
編集 2: 問題を絞り込むことができたので、これは少し混乱し、読みにくかったため、新しいトピックを作成しました: ViewController クラスは、ビューの作成時に新しいインスタンスを作成しますが、既存のものを使用する必要があります
編集 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;
// }

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

1 に答える 1

1

私はこれをいくつかの方法で変更します...

resourcesAsDictionaryのiVarを削除し、代わりにプロパティとして設定します。

@property (nonatomic, strong) NSDictionary *resourcesAsDictionary;

次に、initメソッドを削除し、代わりにこのプロパティを使用して、初期化を変更します。

このコントローラー(つまり、initWithDictionaryを呼び出す場所)にプッシュするviewControllerで...

initWithDictionaryの代わりに...

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

次に、プロパティを設定します...

tvc.resourcesAsDictionary = mySourceDictionary;

次に、TVCをプッシュします...

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

...または、これを画面にプッシュします。

最後に、numberOfRows...コードを...に変更する必要があります。

return [self.resourcesAsDictionary count];

これが私のアプローチであり、UITableViewControllerクラスの初期化をオーバーライドする必要がないことを意味します。

強力なプロパティを持つことにより、所有するオブジェクトの存続期間中、それが存続することを保証します。また、initをオーバーライドしないことで、initWithStyleで呼び出されるコードを見逃さないようにします...

これがうまくいかない場合は、そもそも辞書の作成方法に問題があるとしか思えません。

-(void)viewWillAppear ...関数で、...の出力を教えてください。

NSLog(@"%@", self.resourcesAsDictionary);
于 2012-10-01T14:41:38.990 に答える