自定义cell:可以手动也可以使用xib布局。
当我们的每一行cell的内容不一样,会有变化时,例如微信的朋友圈,可以配图,也可以不配图,所以这个配图的UIImageView的frame就不一样。(因为hidden属性是会占空间的),这时我们使用代码自定义cell。
当每一行cell的格式固定,不存在变化时,使用xib自定义cell就更加便捷了。
- (void)awakeFromNib
{
// Initialization code
//这个方法是在 xib上的控件加载完成后,调用的一个方法,在这个方法中,我们可以重新设置控件的属性,或者添加控件到cell的contentView上。利用这个方法,可以实现xib和代码两种相结合的方式自定义cell
}
使用XIB自定义Cell时,创建cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * idn=@"AZFirstTableViewCell";//必须与xib中的标示符一致
static BOOL isRegistr=NO;
if (!isRegistr) {
//在UITableView中注册自定义UITableViewCell(xib)
UINib *nib=[UINib nibWithNibName:@"AZFirstTableViewCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:idn];
isRegistr=YES;
}
//自定义cell的复用
AZFirstTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:idn];
AZAppModel *app=self.cellArray[indexPath.row];
cell.iconImageView.image=[UIImage imageNamed:app.icon];
cell.nameLabel.text=[NSString stringWithFormat:@"%d%@",indexPath.row,app.name];
cell.testLabel.text=@"ok";
代码自定义cell:
思路:
01 重写 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier这个方法,然后在其中创建UI控件(先不指定frame),指明每个控件的tag值,添加到tablecell的contentView上。
02 定义一个setCellFrame的接口,利用tag值查找到每个UI控件,然后根据传入的model(比如是否有配图),来确定每个控件的frame。
#import "AZMyTableViewCell.h"
#define ICONTAG 10
#define NAMETAG 11
#define CONTENTTAG 12
#define PICTURETAG 13
@implementation AZMyTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIImageView *icon=[[UIImageView alloc] init];
icon.tag=ICONTAG;
[self.contentView addSubview:icon];
UILabel *nameLabel=[[UILabel alloc] init];
nameLabel.tag=NAMETAG;
nameLabel.font=[UIFont systemFontOfSize:15];
[self.contentView addSubview:nameLabel];
UILabel *contentLabel=[[UILabel alloc] init];
contentLabel.tag=CONTENTTAG;
contentLabel.numberOfLines=2;
contentLabel.font=[UIFont systemFontOfSize:12];
[self.contentView addSubview:contentLabel];
UIImageView *picture=[[UIImageView alloc] init];
picture.tag=PICTURETAG;
[self.contentView addSubview:picture];
}
return self;
}
-(void)cellFrame:(AZWeiBo *)weiBo
{
UIImageView *icon=(UIImageView *)[self.contentView viewWithTag:ICONTAG];
icon.frame=CGRectMake(2, 2, 40, 40);
UILabel *nameLael=(UILabel *)[self.contentView viewWithTag:NAMETAG];
nameLael.frame=CGRectMake(45, 2, 150, 20);
UILabel *contentLabel=(UILabel *)[self.contentView viewWithTag:CONTENTTAG];
contentLabel.frame=CGRectMake(45, 25, 200, 40);
UIImageView *picture=(UIImageView *)[self.contentView viewWithTag:PICTURETAG];
if (weiBo.picture==nil) {
picture.hidden=YES;
self.cellHight=60;
}
else{
picture.hidden=NO;
picture.frame=CGRectMake(45, 70, 60, 60);
self.cellHight=132;
}
}
-(void)setWeiBo:(AZWeiBo *)weiBo
{
UIImageView *icon=(UIImageView *)[self.contentView viewWithTag:ICONTAG];
icon.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",weiBo.icon]];
UILabel *nameLael=(UILabel *)[self.contentView viewWithTag:NAMETAG];
nameLael.text=weiBo.name;
UILabel *contentLabel=(UILabel *)[self.contentView viewWithTag:CONTENTTAG];
contentLabel.text=weiBo.content;
UIImageView *picture=(UIImageView *)[self.contentView viewWithTag:PICTURETAG];
if (weiBo.picture!=nil)
{
picture.hidden=NO;
picture.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",weiBo.picture]];
}
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end