【自定义Cell2】.所有经过代码添加:缓存
【1】:设置Cellatom
1):建立一个MsgCell类 继承UITableViewCellcode
.h中声明2个属性一个是用户头像,另一个是发表的文字orm
@property (nonatomic,weak,readonly) UIImageView *iconView; // 用户头像 @property (nonatomic,weak,readonly) UIButton *msgBtn; // 发表的文字
.m中,建立出具体的cell内容blog
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 添加子控件 1,左边的头像 UIImageView *iconView = [[UIImageView alloc] init]; iconView.frame = CGRectMake(10, 10, 50, 50); [self addSubview:iconView]; [self.contentView addSubview:iconView]; _iconView=iconView; // 添加右边的对话按钮 UIButton *btn = [[UIButton alloc]init]; btn.frame = CGRectMake(100, 0, 100, 60); [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_normal.png"] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_focused.png"] forState: UIControlStateHighlighted]; [self.contentView addSubview:btn]; // 设置文字颜色 [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _msgBtn=btn; } return self; }
建立一个ViewController:UITableViewController继承
#import "ViewController.h" #import "MsgCell.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 去除横线 分割线 self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; self.tableView.editing = YES; }// 几行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; // 20个cell } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 用static修饰的局部变量,只会初始化一次 static NSString *ID=@"Cell"; // 2.拿到一个标识先去缓存池中查找对应的Cell MsgCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; // 3. 若是缓存池中没有,才须要建立新的标识 if(cell==nil) { cell=[[MsgCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.iconView.image=[UIImage imageNamed:@"0.png"]; [cell.msgBtn setTitle:@"哈哈哈" forState:UIControlStateNormal]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 每一行的高度自定义 return 70; } @end