0

私のUIViewnibファイルには、画面の約半分を占めるU​​ITableViewがあります。対応する.hファイルと.mファイルは次のとおりです。

// helloViewController.h
#import <UIKit/UIKit.h>
@interface helloViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    NSArray *locationArray;
}
@property (nonatomic, retain) NSArray *locationArray;
@end

// helloViewController.m
@synthesize locationArray;
- (void)viewDidLoad {
    locationArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8",  @"9",  @"10",  @"11", nil];
    [super viewDidLoad];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    [[cell textLabel] setText: [locationArray objectAtIndex:[indexPath row]]];

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

これを実行すると、テーブルをスクロールしようとするとクラッシュします(デバッガーでエラーは発生しません)。ただし、[[cell textLabel] setText:[locationArray objectAtIndex:[indexPath row]]];を置き換えると、と

[[cell textLabel] setText:@"Boom"];

...クラッシュしません...

この問題の原因は何ですか?デリゲートとデータソースはIBのファイルの所有者に接続されており、ファイルの所有者のクラスは正しいクラスに設定されています。ペン先のuiview内でテーブルビューを使用しているのは問題ですか?

4

3 に答える 3

1

問題はlocationArray、で自動解放されたオブジェクトに設定していることですviewDidLoad。次に、セルを設定する場所でこのオブジェクトに再度アクセスしようとしますが、それまでに配列の割り当てが解除されています。

定義したretain-property(プロパティを使用せずに配列を直接設定している)を使用し、メモリ管理についてさらに読む必要があります。;)

self.locationArray = [NSArray arrayWith...];
于 2009-12-05T20:08:47.830 に答える
0

これは、locationArrayが以前は保持されておらず、現在はガベージメモリを指していることが原因である可能性があります。つまり、locationArrayには、指定されたインデックスのアイテムが含まれていません。

于 2009-12-05T20:07:00.330 に答える
0

はい、それは配列のメモリが保持されているためです。UはdeallocメソッドでLocation配列Ivarを解放する必要があります。このコードを使用してください

NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];
self.locationArray = array ;
[array release];

Deallocメソッドで

 [locationArray release];
于 2012-04-02T10:45:50.217 に答える