当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS-自定义cell

iOS-自定义cell

2020年07月27日  | 移动技术网移动技术  | 我要评论

思路

  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中定义好的属性中添加值就好了。

本文地址:https://blog.csdn.net/weixin_45747214/article/details/107599463

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网