1

.plistファイルから情報を読み取ろうとするアプリを作成しています (そこに解析されたJSONを配置します)。

ファイルフローからの読み取りはうまくいきます。辞書の配列を取得しましたが、それをテーブルビューに表示しようとすると、問題が発生します。最初のビューは正しく読み込まれますが、スクロールを開始するとアプリがクラッシュします。

#define DOCUMENTS [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]

- (void)viewDidLoad
{
    [super viewDidLoad];    
    NSString *filePathDocArray = [DOCUMENTS stringByAppendingPathComponent:@"filters.plist"];
    NSString *filePathBundleArray = [[NSBundle mainBundle] pathForResource:@"filters" ofType:@"plist"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:filePathDocArray]) {
        [[NSFileManager defaultManager] copyItemAtPath:filePathBundleArray toPath:filePathDocArray error:nil];
        NSLog(@"File saved");
    } else {
        NSLog(@"File already exists");
        filters = [NSArray arrayWithContentsOfFile:filePathDocArray];
    }

}

ここで、必要なすべての情報をフィルター配列に取得します (ループによってチェックされます)。それで:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [filters count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myIdentifier];                              
    }

    NSInteger crow = indexPath.row;
    NSDictionary *story = [filters objectAtIndex: crow];
    cell.textLabel.text = [story objectForKey:@"Name"];
    cell.detailTextLabel.text = [story objectForKey:@"Description"];
    return cell;
}
@end

アプリの起動時は問題ありません: 通常のテーブル ビューが表示されますが、スクロールを開始するとクラッシュします

私が評価した一連のブレークポイント デバッグの後、アプリケーションが Simulator で起動した後、配列フィルターのリンクがネジ止めされるため、次のセルに入力しようとすると、ストーリーディクショナリが適切に作成されません。

どのような問題が考えられますか?

コンソールレポートは次のとおりです。

2012-09-22 13:37:43.545 JSONExample[4559:207] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a083c0

2012-09-22 13:37:43.547 JSONExample[4559:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a083c0'

4

4 に答える 4

0

別のことを覚えておく必要があります..このステップを実行する場合

 NSDictionary *story = [[filters objectAtIndex: crow]retain];

cellForRowAtIndexPath が呼び出されるたびに、保持カウントが 1 ずつ増加します。ただし、デロックは 1 回だけです。そのため、アプリケーションでメモリ リークが発生します。メモリ管理ガイドを一度読むことをお勧めします。その小さなドキュメント。最大で 1 日かかります。しかし、何をすべきか、何をすべきでないかについて、より自信を持つようになります。

http://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.pdf

乾杯!!そしてハッピーコーディング!!

于 2012-09-22T11:52:07.667 に答える
0

あなたが同じようにプロパティを書いた場合は、次のfiltersよう@property (nonatomic, retain) NSArray *filters;に書く必要がありますself.filters = [NSArray arrayWithContentsOfFile:filePathDocArray];

それ以外の場合は、次のように書く必要がありますfilters = [[NSArray arrayWithContentsOfFile:filePathDocArray] retain];

于 2012-09-22T10:38:15.233 に答える
0

-[NSCFString objectAtIndex:]: 認識されないセレクターも同じ問題のようです。

「filters」変数は、呼び出す時点で NSString/CFStringobjectAtIndex:です。想定されるように、配列ではありません。リンク先の質問に示されている解決策は、retain設定されているときは常にフィルター配列です。

于 2012-09-22T10:06:56.800 に答える
0

ARCを使用していますか?? そうでない場合は、最初にセルを自動解放してください!! そして、フィルターが保持プロパティでない場合..親切にそれを1つにして合成し、ARCが使用されていない場合は、deallocブロックで解放します..そうするべきだと思います..

于 2012-09-22T10:10:39.823 に答える