少し混乱しています。配列をtableViewdataSourceメソッドnumberOfRowsInSectionに出力すると、アプリがクラッシュします。
これは私のコードです:.hファイル内
@interface AddColor : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tblView;
NSArray *arrayColors;
}
@property(nonatomic,retain)NSArray *arrayColors;
@end
.mファイル内
@synthesize arrayColors;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO];
arrayColors = [NSArray arrayWithObjects:@"blackColor", @"darkGrayColor", @"lightGrayColor", @"whiteColor", @"grayColor", @"redColor", @"greenColor", @"blueColor", @"cyanColor", @"yellowColor", @"magentaColor", @"orangeColor", @"purpleColor", @"brownColor", nil];
tblView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
tblView.delegate=self;
tblView.dataSource=self;
[self.view addSubview:tblView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"%d",[arrayColors count]);//App crashes here.
return [arrayColors count];
}
[arrayColors count]を印刷すると、アプリがクラッシュします。
クラッシュの解決策を見つけました。配列をviewDidLoadに保持するだけです。
[arrayColor retain];
そして今はうまく働いています。しかし、[arrayColor count]を印刷する前に、アプリがクラッシュするのはなぜですか。