创建一个继承于 UITableViewCell 的 我在这里取名为 “自定义cell” 在 自定义cell.h里面写好要写的东西 , 比如 label imageView 等 自定义cell.m里面写 例如
//重写图片 -(UIImageView *)imgv{ if(!_imgv){ _imgv=[[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 80, 80)]; _imgv.layer.cornerRadius=40; _imgv.layer.masksToBounds=YES; } return _imgv; }
或者
//重写主标题 -(UILabel *)zla{ if(!_zla){ _zla=[[UILabel alloc]initWithFrame:CGRectMake(130, 15, 80, 50)]; _zla.font=[UIFont systemFontOfSize:20]; _zla.textColor=[UIColor orangeColor];
} return _zla;} //重写副标题 -(UILabel *)zla2{ if(!_zla2){ _zla2=[[UILabel alloc]initWithFrame:CGRectMake(130, 55, 80, 50)]; _zla2.font=[UIFont systemFontOfSize:15]; _zla2.textColor=[UIColor greenColor]; } return _zla2; }
然后 重写构造方法
//重写构造方法 -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ //先去重新父类 ->调用父类的方法 if(self==[super initWithStyle:style reuseIdentifier:reuseIdentifier]){ //cell添加子控件 [self addSubview:self.imgv]; [self addSubview:self.zla]; [self addSubview:self.zla2]; [self addSubview:self.shi]; } return self; //cell }
回到写表格的界面
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法里 这样创建Cell
自定义Cell *cell=[tableView dequeueReusableCellWithIdentifier:@“cell”]; if(cell==nil){ cell=[[自定义Cell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“cell”]; }
属性
//新属性 cell.imgv.image=[UIImage imageNamed:arr3[indexPath.row]]; //主标题 cell.zla.text=arr[indexPath.row]; //副标题 cell.zla2.text=arr2[indexPath.row]; //时间 cell.shi.text=arr4[indexPath.row];