iOS-自定义cell

思路

  1. 创建一个WriteTableViewCell文件继承于UITableViewCell
  2. 在WriteTableViewCell.h文件中定义cell中需要的属性
  3. 在WriteTableViewCell.m文件中写方法
  4. 在WriteViewController.m中添加协议函数,可以访问自定义cell

具体代码

  • WriteTableViewCell.h文件中
@interface WriteTableViewCell : UITableViewCell

@property(nonatomic, strong) UIImageView *picture;
@property(nonatomic, strong) UILabel *lbName;
@property(nonatomic, strong) UIButton *btLook;

@end
  • WriteTableViewCell.m中
-(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);
}
  • WriteViewController.h文件中
@interface WriteViewController : UIViewController
@property UITableView *tableView;//创建一个tableView属性
@end
  • WriteViewController.m文件中
#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中定义好的属性中添加值就好了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值