GUIでの作業
- ビューの追加
- NewFileからUIViewControllerのサブクラスを新規に作成。名称はPhotoCellControllerのようにする
- 生成されたxibファイルを開き、UIViewを消し、Objects一覧からテーブルセル単体をD&Dする
- セル上に必要な画面構成要素(UIImageViewやUILabel、UIButtonなど)を好きに配置する
- ↓の手順に従い、コードを変更する
- アシスタントエディタを開き、PhotoCell内の要素をPhotoCellクラスにアウトレット接続する。PhotoCell自体をPhotoCellControllerにアウトレット接続する。
コードの変更作業
- 上記4.の手順
- PhotoCellController.hで、PhotoCellControllerの上にPhotoCellクラスを書く
- PhotoCellController.mで、PhotoCellControllerの上にPhotoCellクラスを実装する
PhotoCellController.h
@interface PhotoCell : UITableViewCell {
@private
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@end
@interface PhotoCellController : UIViewController
{
@private IBOutlet PhotoCell* _cell;
}
@end
PhotoCellController.m
# import "PhotoCellController.h"
@implementation PhotoCell
@synthesize imageView;
@synthesize nameLabel;
- (void)dealloc
{
self.imageView = nil;
self.nameLabel = nil;
}
@end
@implementation PhotoCellController
〜〜……
- MasterViewController.mでカスタムセルを使う部分の変更
- #import "PhotoCellController.h"し、UITableViewCellの部分を以下のように変更する
MasterViewController.m
@interface MasterViewController ()
- (void)configureCell:(PhotoCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
// Customize the appearance of table view cells.
- (PhotoCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PhotoCell";
PhotoCell *cell = (PhotoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
PhotoCellController* controller = [[PhotoCellController alloc] initWithNibName:@"PhotoCellController" bundle:nil];
cell = (PhotoCell *)controller.view;
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(PhotoCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
〜〜
}