当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS—自定义cell及两种复用方式

iOS—自定义cell及两种复用方式

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

首先,在这里反省一下自己,在遇到问题的时候没有及时去解决,这一点很不好,得改

一、关于UITableView 复用cell两种方法:

  • 非注册
    -(id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
    如果没有复用cell,程序可能会返回nil, 所以创建完cell后必须要做判空处理,未获取到则重新创建。这种方法可以不用注册。
 //尝试获得可以复用的单元格。但不一定获得到。得不到返回nil
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ft"];
        
    //如果没有获得到
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ft"];
    }
  • 注册
    (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
    获取复用的cell,如果没有复用的cell,将自动使用提供的class类创建新的cell并返回,后面不需要再进行判空。在此之前必须要注册cell。
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"ft"];

MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ft" forIndexPath:indexPath];

  • 区别:
    注册的方法需要提前将要复用的 Cell 类注册,而不需要在获取 Cell 时手动判断 cell的获取 是否为nil,这是因为 dequeueReusableCellWithIdentifier:identifier forIndexPath:在内部处理了这个过程,使得最后返回的一定是可用的 Cell
    具体看这篇

二、步骤

建立MyTableViewCell文件继承于UITableViewCell

// MyTableViewCell.h文件
@interface MyTableViewCell : UITableViewCell
@property UILabel *label;
@property UIButton *btn;
@end

//MyTableViewCell.m文件

#import "MyTableViewCell.h"

@implementation MyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if ([self.reuseIdentifier isEqualToString:@"ft"]) {
        _label = [[UILabel alloc] init];
        [self.contentView addSubview:_label];
        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_btn];
        
    }
    return self;
}

//布局
- (void)layoutSubviews {
    _label.frame = CGRectMake(30, 10, 100, 50);
    _btn.frame = CGRectMake(100, 10, 50, 50);
}


@end

在ViewController.h 中添加协议 定义数据视图对象

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,

//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>

//定义数据视图对象。数据视图用来显示大量相同格式的大量信息的视图
@property UITableView *tableView;

@end


ViewController.m文件:

#import "ViewController.h"
#import "MyTableViewCell.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //注册 使用- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;必须注册
    [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"ft"];
  
 
}

//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 2;
}
 
 //组内行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

//单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

//设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ft" forIndexPath:indexPath];
    //MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ft"];
    //if(cell == nil) {
    //    cell = [[MyTableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"ft"];
    //}
	
	//以上为UITableView 重用cell两种方法
    if(indexPath.section == 0) {
        cell.label.text = @"ft";
        [cell.btn setImage:[UIImage imageNamed:@"3.jpg"] forState:UIControlStateNormal];
    } else {
        cell.label.text = @"ff";
        [cell.btn setImage:[UIImage imageNamed:@"5.jpg"] forState:UIControlStateNormal];
    }

    return cell;    
}

@end

运行:
1

本文地址:https://blog.csdn.net/qq_45836906/article/details/107455365

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

相关文章:

验证码:
移动技术网