【iOS】——自定义cell的方式

在学习UITableView时遇到了cell的复用,一开始接触时不是很理解,在之后用到时很不熟练,所以重新学习了cell的方式,然后写博客总结了一下。

参考博客:
[iOS开发]TableviewCell复用机制学习

复用cell的两种方式分为注册和不注册,二者的区别主要是:注册情况下需要cell时不需要手动判断cell的获取是否为空,因为注册时dequeueReusableCellWithIdentifier:forIndexPath:在内部处理了这个过程,使得最后返回的都是可用的cell。

注册:

注册主要是为了在获取复用的cell时,如果没有可复用的cell时,系统将会自动的使用注册时提供的类来创建可用的cell,这样确保了返回的一定是可用的cell。

[self.tableView registerClass:[ViewController_01 class] forCellReuseIdentifier:@"1"];
[_tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];

使用这种方法时,须重写系统的-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法,设置自己需要的cell样式。

示例:
TableViewCell.m

-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if([self.reuseIdentifier isEqualToString:@"1"]) {
        _label = [[UILabel alloc] init];
        [self.contentView addSubview:_label];
        _label.text = @"1";
        _tempimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"/Users/haoqianbiao/Desktop/test1/UI/cell的复用/5.png"]];
        [self.contentView addSubview:_tempimageView];
    } else {
        _label = [[UILabel alloc] init];
        [self.contentView addSubview:_label];
        _label.text = @"2";
        _tempimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"/Users/haoqianbiao/Desktop/test1/UI/cell的复用/6.png"]];
        [self.contentView addSubview:_tempimageView];
    }
    return self;
}
- (void)layoutSubviews {
    _label.frame = CGRectMake(150, 20, 100, 49);
    _tempimageView.frame = CGRectMake(10, 0, 50, 50);
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"1"];
    [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"2"];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row % 2 == 0) {
        TableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
        return cell;
    } else {
        TableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"2" forIndexPath:indexPath];
        return cell;
    }
}

未注册

未注册时就需要判断返回的cell是否为空,若果为空,则为它创建一个新的cell。

    NSString* str = @"cell";
    
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:str];
    
    if(cell == nil) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }

总结:

注册与不注册本质上其实是是否可以返回一个可用的cell,注册后,有一个class与identifier绑定,所以在获取时,系统就可以自动创建出指定样式的cell;如果没有注册就需要手动判断返回的cell是否为空,若为空,则手动创建一个新的cell,也就是说当注册后就不需要去考虑cell的新建问题了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值