Life is Really Short, Have Your Life!!

ござ先輩の主に技術的なメモ

cellForRowAtIndexPathのスクロールでクラッシュした件のまとめ

cellForRowAtIndexPathでのスクロール処理でクラッシュ・・・ - Life is Really Short, Have Your Life!!のまとめです。

コメントくださった、@ikmさんと@k_katsumiさん、ありがとうございます。お前のコードだめだぞって言ってもらえるのが嬉しい限りです。

現在このコードで動いております、ありがとうございますというご報告です。対象箇所だけ抜粋します。

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

//HogeController.m
#import "MainMenuCell.h"
@implementation HomeViewController
@synthesize cellData;
- (void)viewDidLoad {
//データの初期化
	cellData = [[NSMutableArray alloc]initWithCapacity:30];
	for (int i=0; i<30; i++) {
		NSDictionary *left = [NSDictionary dictionaryWithObjectsAndKeys:
							  @"F86966",@"itemcode",
							  @"100",@"stocknum",
							  @"300",@"ordernum",
							  nil
							  ];
		NSDictionary *right 
		= [NSDictionary dictionaryWithObjectsAndKeys:
		   @"F86967",@"itemcode",
		   @"50",@"stocknum",
		   @"1000",@"ordernum",
		   nil
		   ];
		NSArray *arr = [[NSArray alloc]initWithObjects:left,right,nil];
		[cellData addObject:arr];
		[arr release];
	}
}
// create table cell view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *identifier = @"MainMenuCell";
        //カスタムセル。IBを使用。
        //see:http://d.hatena.ne.jp/KishikawaKatsumi/20081024/1224857278
	MainMenuCell *mainCell= (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:identifier];	
	if (mainCell == nil) {
		MainMenuCellController *controller = [[MainMenuCellController alloc] initWithNibName:identifier bundle:nil];
		mainCell = (MainMenuCell *)controller.view;		
        [controller release];

		NSArray *arr = [cellData objectAtIndex:indexPath.row];
		NSDictionary *left = [arr objectAtIndex:0];
		NSDictionary *right = [arr objectAtIndex:1];
	
		[mainCell.left_foo setText:[left objectForKey:@"foo"]];
		[mainCell.left_bar setText:[left objectForKey:@"bar"]];
		[mainCell.left_hoge setText:[left objectForKey:@"hoge"]];
		
		[mainCell.right_foo setText:[right objectForKey:@"foo"]];
		[mainCell.right_bar setText:[right objectForKey:@"bar"]];
		[mainCell.right_hoge setText:[right objectForKey:@"hoge"]];				
	}
   
	mainCell.selectionStyle = UITableViewCellSelectionStyleNone;
	return mainCell;	
}
@end

analyize結果は問題なしで、スクロール処理も快適に動いております。