【iOS】——自定义UITableViewCell


一、实现方式

我们可以通过继承 UITableViewCell 并重写其中的一些方法来实现自定义 UITableViewCell。并且分为使用注册和不注册两种方式来实现。下面分别详细讲述这两种方式的实现方法:

注册方式:
它的主要思路是在 viewDidLoad 方法中注册自定义 UITableViewCell 的类,以便在需要时从可重用池中获取它。这种方式的实现比较复杂,但由于每次可以从可重用池中获取实例,因此在数据量较大时性能更好。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册自定义 UITableViewCell 的类
    [self.tableView registerClass:[myCustomCell class] forCellReuseIdentifier:@"cellIdentifier"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    myCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    cell.titleLabel.text = @"Title";
    cell.subtitleLabel.text = @"Subtitle";
    return cell;
}

不注册方式
它的主要思路是在 tableView:cellForRowAtIndexPath: 方法中手动创建自定义 UITableViewCell 的实例。这种方式的实现比较简单,但由于每次需要手动创建实例,因此在数据量较大时会影响性能。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    myCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[myCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.titleLabel.text = @"Title";
    cell.subtitleLabel.text = @"Subtitle";
    return cell;
}

两者的区别是在tableView:cellForRowAtIndexPath: 方法中注册使用 dequeueReusableCellWithIdentifier:forIndexPath: 方法来获取可重用的 UITableViewCell,
不注册使用
dequeueReusableCellWithIdentifier: 方法获取可重用的 UITableViewCell并且还需要判断其获取的实例是否为nil。
这是因为这是因为在注册方式下,每个 UITableViewCell 的实例都已经被注册到了可重用池中,并与对应的重用标识符关联起来而不注册则不是。

二、实现代码

首先创建一个继承于UITableViewCell类的子类,接着在子类中定义我们所需的属性

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface myCustomCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@end

NS_ASSUME_NONNULL_END

接着在类的实现部分重写父类的initWithStyle:reuseIdentifier:方法和layoutSubviews方法。

#import "myCustomCell.h"

@implementation myCustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
//重写父类的初始化方法,根据需求添加自己的逻辑
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style  reuseIdentifier:reuseIdentifier];
    if ([reuseIdentifier isEqualToString:@"indentifierCell"]) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_titleLabel];
        
        _subtitleLabel = [[UILabel alloc] init];
        _subtitleLabel.textColor = [UIColor grayColor];
        _subtitleLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_subtitleLabel];
    }
    return self;
}
//重写布局方法,根据需求自己设置
- (void)layoutSubviews {
    _titleLabel.frame = CGRectMake(20, 10, self.contentView.bounds.size.width - 40, 20);
    _subtitleLabel.frame = CGRectMake(20, 30, self.contentView.bounds.size.width - 40, 20);
}
@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
{
    //定义一个数据视图对象
    //数据视图用来显示大量相同格式的信息的视图
    //例如:电话通讯录、QQ好友、微信朋友圈
    UITableView* _tableview;
}

@end

ViewController.m

#import "ViewController.h"
#import "myCustomCell.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     //创建数据视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
     //设置数据视图的代理对象
    _tableView.delegate = self;
     //设置数据视图的数据源对象
    _tableView.dataSource = self;
    //注册子类
    [_tableView registerClass:[myCustomCell class] forCellReuseIdentifier:@"indentifierCell"];
    [self.view addSubview:_tableView];
}
// 设置数据视图的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
//获取每组元素的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 15;
}
//创建单元格对象函数
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    myCustomCell* cell = [_tableView dequeueReusableCellWithIdentifier:@"indentifierCell" forIndexPath:indexPath];
    cell.titleLabel.text = @"主标题";
    cell.subtitleLabel.text = @"副标题";
    return cell;
}
@end

运行结果如下:

请添加图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值