@interface WriteTableViewCell : UITableViewCell
@property(nonatomic, strong) UIImageView *picture;
@property(nonatomic, strong) UILabel *lbName;
@property(nonatomic, strong) UIButton *btLook;
@end
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if ([self.reuseIdentifier isEqualToString:@"01"]){
_picture = [[UIImageView alloc]init];
[self.contentView addSubview:_picture];
_lbName = [[UILabel alloc]init];
[self.contentView addSubview:_lbName];
}
else{
_picture = [[UIImageView alloc]init];
[self.contentView addSubview:_picture];
_lbName = [[UILabel alloc]init];
[self.contentView addSubview:_lbName];
_btLook = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:_btLook];
}
return self;
}
-(void) layoutSubviews{
_picture.frame = CGRectMake(0, 0, 155, 155);
_lbName.frame = CGRectMake(190, 50, 100, 30);
_lbName.font = [UIFont systemFontOfSize:12];
_btLook.frame = CGRectMake(285, 125, 25, 15);
}
@interface WriteViewController : UIViewController
@property UITableView *tableView;//创建一个tableView属性
@end
#import "WriteViewController.h"
#import "WriteTableViewCell.h"
@interface WriteViewController ()
<UIScrollViewDelegate, UITableViewDelegate>//添加两个协议文件
@end
@implementation WriteViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化tableView
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 820) style:UITableViewStylePlain];
//添加到视图
[self.view addSubview:self.tableView];
//设置代理
self.tableView.dataSource = self;
self.tableView.delegate = self;
//注册自定义cell
[self.tableView registerClass:[ScrollerTableViewCell class] forCellReuseIdentifier:@"00"];
//如果存在其他的自定义cell,可以通过CellReuseIdentifier来区分
[self.tableView registerClass:[ScrollerTableViewCell class] forCellReuseIdentifier:@"01"];
}
//设定每个Section的行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
//设置Section数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 5;
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 155;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//可以通过if语句为不同行的cell添加不同的图片和文字
if (indexPath.section == 0){
WriteTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"00" forIndexPath:indexPath];
UIImage *pic = [UIImage imageNamed:@"list_img1.png"];
cell.picture.image = pic;
cell.lbName.text = @"小王";
return cell;
}
else if (indexPath.section == 1){
WriteTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"00" forIndexPath:indexPath];
UIImage *pic = [UIImage imageNamed:@"list_img2.png"];
cell.picture.image = pic;
cell.lbName.text = @"小李";
return cell;
}
else{
WriteTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"01" forIndexPath:indexPath];
UIImage *pic = [UIImage imageNamed:@"list_img3.png"];
cell.picture.image = pic;
[cell.btLook setImage:[UIImage imageNamed:@"button_01.png"] forState:UIControlStateNormal];
cell.lbName.text = @"小刘";
return cell;
}
}
如果有多个不同的自定义cell, 可以将不同的cell放在同一个类中,也可以放在不同的类中,声明方法与前面相似。
个人感觉自定义cell就相当于自己创建了一个模版,当有许多相似的cell时,创建一个固定模版cell之后在ViewController中使用时直接想cell中定义好的属性中添加值就好了。
本文地址:https://blog.csdn.net/weixin_45747214/article/details/107599463
如对本文有疑问, 点击进行留言回复!!
牛客编程巅峰赛S1第6场 - 黄金&钻石&王者题解
纵横字谜的答案 Crossword Answers, ACM/ICPC World Finals 1994, UVa232
HDU - 5880 Family View (AC自动机修改母串)
iOS14Beta3续航怎么样 iOS14Beta3续航能力介绍
iOS14Beta3稳定性怎么样 iOS14Beta3升级建议介绍
网友评论