ここにサンプルがあります、それは私のコードの1つです.:-
@interface CustomizedCellProductDetails : UITableViewCell {
UILabel *sNO;
UILabel *abcWine;
UILabel *redWine;
UILabel *two;
UILabel *hundred;
UILabel *fourTwo;
UILabel *twoOne;
UIImageView *imgView;
UILabel *itemNo;
UILabel *itemName;
UILabel *itemDesc;
UILabel *department;
UILabel *qtyAvailable;
UIButton *check;
}
@property (nonatomic , retain) UILabel *sNO;
@property (nonatomic , retain) UILabel *abcWine;
@property (nonatomic , retain) UILabel *redWine;
@property (nonatomic , retain) UILabel *two;
@property (nonatomic , retain) UILabel *hundred;
@property (nonatomic , retain) UILabel *fourTwo;
@property (nonatomic , retain) UILabel *twoOne;
@property (nonatomic , retain) UIImageView *imgView;
@property (nonatomic , retain) UILabel *itemNo;
@property (nonatomic , retain) UILabel *itemName;
@property (nonatomic , retain) UILabel *itemDesc;
@property (nonatomic , retain) UILabel *department;
@property (nonatomic , retain) UILabel *qtyAvailable;
@property (nonatomic , retain) UIButton *check;
-(void) clicked;
@end
import "CustomizedCellProductDetails.h"
@implementation CustomizedCellProductDetails
@synthesize sNO,abcWine,redWine,two,hundred,fourTwo,twoOne,imgView,itemNo,itemName,itemDesc,department,qtyAvailable,check;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
sNO=[[UILabel alloc] init];
abcWine=[[UILabel alloc] init];
redWine=[[UILabel alloc] init];
two=[[UILabel alloc] init];
hundred=[[UILabel alloc] init];
fourTwo=[[UILabel alloc] init];
twoOne=[[UILabel alloc] init];
imgView=[[UIImageView alloc] init];
itemNo=[[UILabel alloc] init];
itemName=[[UILabel alloc] init];
itemDesc=[[UILabel alloc] init];
department=[[UILabel alloc]init];
qtyAvailable=[[UILabel alloc] init];
check=[UIButton buttonWithType:UIButtonTypeCustom];
[check addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[check setTitle:@"Check" forState:UIControlStateNormal];
[self.contentView addSubview:sNO];
[self.contentView addSubview:abcWine];
[self.contentView addSubview:redWine];
[self.contentView addSubview:two];
[self.contentView addSubview:hundred];
[self.contentView addSubview:fourTwo];
[self.contentView addSubview:twoOne];
[self.contentView addSubview:itemNo];
[self.contentView addSubview:itemName];
[self.contentView addSubview:itemDesc];
[self.contentView addSubview:department];
[self.contentView addSubview:qtyAvailable];
[self.contentView addSubview:check];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame=CGRectMake(boundsX+10, 0, 50, 40);
sNO.frame = frame;
frame= CGRectMake(boundsX+70 ,0, 150, 40);
abcWine.frame = frame;
frame= CGRectMake(boundsX+230 ,0, 150, 40);
redWine.frame = frame;
frame= CGRectMake(boundsX+390 ,0, 50, 40);
two.frame = frame;
frame= CGRectMake(boundsX+450 ,0, 50, 40);
hundred.frame = frame;
frame= CGRectMake(boundsX+510 ,0, 50, 40);
fourTwo.frame = frame;
frame= CGRectMake(boundsX+570 ,0, 50, 40);
twoOne.frame = frame;
/************************ ***********************/
frame= CGRectMake(boundsX+10 ,50, 100, 200);
imgView.frame = frame;
}
-(void) clicked
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
@end
cellforRowAtIndexPath でこのクラスのインスタンスを使用し、必要に応じて変更するだけです。実行時に値を変更するには、プロパティにアクセスします。
この方法でカスタムcemmをテーブルビューに実装できます:-
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomizedCellProductDetails *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomizedCellProductDetails alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.sNO.text=@"2255";
cell.abcWine.text=@"ABCWine";
cell.redWine.text=@"REDWine";
cell.two.text=@"2";
cell.hundred.text=@"100";
cell.fourTwo.text=@"42";
cell.twoOne.text=@"21";
[cell.imgView setImage:[UIImage imageNamed:@"wine.png"]];
cell.itemNo.text=@"Item No 2255";
cell.itemName.text=@"Item Name ABCWine";
cell.itemDesc.text=@"Item Description This is the best wine";
cell.qtyAvailable.text=@"Quantity Available 2";
// Configure the cell...
return cell;
}
セルの高さを変更します:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300;
}
代わりに 300 を使用しないでください。セルのフレームの高さを次のように指定します。cell.frame.size.height