纯代码
则tableViewCell类中需实现 initWithStyle:reuseIdentifier:
@implementation MyCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//创建自定义单元格中控件
self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 120, 120)];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 45, 51, 21)];
nameLabel.text = @"姓名:";
self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(239, 45, 51, 21)];
UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 80, 51, 21)];
nicknameLabel.text = @"昵称:";
self.nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(239, 80, 51, 21)];
//加入单元格内容视图中
[self.contentView addSubview:self.myImageView];
[self.contentView addSubview:nameLabel];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:nicknameLabel];
[self.contentView addSubview:self.nicknameLabel];
}
return self;
}
调用该cell的tableView中只需在下面方法中将系统默认的UITableViewCell替换成自己的类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建可重用标识符
static NSString *cellID = @"cellID";
//首先从出列可重用队列中获取单元格
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//如果没有则创建单元格
if (!cell) {
cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
//获取Student对象
Student *stu = self.students[indexPath.row];
// Configure the cell...
//设置自定义单元格的内容
// cell.myImageView.image = stu.icon;
// cell.nameLabel.text = stu.name;
// cell.nicknameLabel.text = stu.nickname;
[cell bind:stu];//绑定数据
return cell;
}
纯xib
新建newFile-userInterface-empty
然后拖入tableViewCell控件。
实现代码如下。其中 if (!isRegister)部分可以放在viewDidLoad中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建可重用标识符
static NSString *cellID = @"cellID";
//注册xib,生成单元格对象,只需注册一次就放入出列可重用队列中,以后直接从出列可重用队列中获取此单元格对象即可
static BOOL isRegister = NO;//是否注册过标识
if (!isRegister) {//如果没有注册过,则注册
UINib *nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:cellID];
isRegister = YES;//设置注册标志位
}
//从出列可重用队列中获取单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
//获取Student对象
Student *stu = self.students[indexPath.row];
// Configure the cell...
//设置自定义单元格的内容
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = stu.icon;
UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
nameLabel.text = stu.name;
UILabel *nickNameLabel = (UILabel *)[cell viewWithTag:102];
nickNameLabel.text = stu.nickname;
return cell;
}
代码+xib
.m .h .xib三者取同样的名字
cell中实现
<span class="s3">//</span><span class="s14">代码加</span><span class="s3">xib</span><span class="s14">混合</span>
- (void)awakeFromNib {
// Initialization code
self.backgroundColor=[UIColor greenColor];
_btn.backgroundColor=[UIColor orangeColor];
}
调用该cell 的tableView中实现
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cell";
//首先从出列可重用队列中获取单元格
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//如果没有才创建单元格对象
if (!cell) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSLog(@"%@",NSStringFromCGRect(cell.contentView.frame));
NSLog(@"%@",NSStringFromCGRect( cell.frame));
return cell;
}