New File/User Interface/Emptyを選び、CustomCell.xib
を作る。
CustomCell.xibをクリックしてStoryBoardで画面作成
TableViewCellをドラッグ
サイズをTableに合わせる
その上にパーツ(ラベル、テキスト、イメージ等)を載せる
NewFileで、CustomCell.h,CustomCell.m作成
// CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell: UITableViewCell{
}
@end
CustomCell.xibをクリック、StoryBoard
Custom ClassをCustomCellに設定
IdentifierにCell名設定
Delegate, Datasourceリンク
.hにIBOUTlet設定
// CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell: UITableViewCell{
}
@property (nonatomic,retain)IBOutlet UILabel *nameLabel;
@property (nonatomic,retain)IBOutlet UILabel *priceLabel;
@property (nonatomic,retain)IBOutlet UILabel *priceGLabel;
@property (nonatomic,retain)IBOutlet UILabel *countLabel;
@property (nonatomic,retain)IBOutlet UIImageView *image;
@property (nonatomic,retain)IBOutlet UIButton *countBtn;
@end
// CustomCell.m
#import <UIKit/UIKit.h>
#include "CustomCell.h"
@implementation CustomCell
@synthesize nameLabel;
@synthesize priceLabel;
@synthesize priceGLabel;
@synthesize countLabel;
@synthesize image;
@synthesize countBtn;
@end
メインの.mファイルでCustom Cell設定
#import "CustomCell.h"
NSMutableArray *_objects;
#define CUSTOM_CELL_NIB @"CustomCell"
float cellheight_;
- (
void)viewDidLoad
{
[super viewDidLoad];
//Table delegate
self.
tableView.
delegate =
self;
//Table data
_objects = [[NSMutableArray alloc] initWithObjects:
@"tabel1",
@"table2",
nil];
//Table Cell Height
UINib* nib = [UINib nibWithNibName:CUSTOM_CELL_NIB bundle:nil];
NSArray* array = [nib instantiateWithOwner:nil options:nil];
CustomCell* cell = [array
objectAtIndex:
0];
cellheight_ =cell.frame.size.height;
}
#pragma mark - Table View
-(
CGFloat)tableView:(
UITableView *)tableView heightForRowAtIndexPath:(
NSIndexPath *)indexPath
{
return cellheight_;
}
//Table section
- (
NSInteger)numberOfSectionsInTableView:(
UITableView *)tableView
{
return 1;
}
//Table row
- (
NSInteger)tableView:(
UITableView *)tableView numberOfRowsInSection:(
NSInteger)section
{
return _objects.
count;
}
//Init Table Data
- (
UITableViewCell *)tableView:(
UITableView *)tableView cellForRowAtIndexPath:(
NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CUSTOM_CELL_NIB];
if (cell ==
nil){
UINib* nib = [
UINib nibWithNibName:
CUSTOM_CELL_NIB bundle:
nil];
NSArray* array = [nib
instantiateWithOwner:
nil options:
nil];
cell = [array
objectAtIndex:
0];
}
NSDate *object =
_objects[indexPath.
row];
//cell.textLabel.text = [object description];
cell.
nameLabel.
text = [object
description];
return cell;
}
//ボタンがタップされたらテーブルに追加する
- (
void)insert:(
UIButton*)btn
{
[_objects insertObject:@"hogehoge" atIndex:0];
NSIndexPath *indexPath = [
NSIndexPath indexPathForRow:
0 inSection:
0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}