小工具知识网

当前位置: 首页 > 行业知识 > 【iOS】——自定义UITableViewCell

行业知识

【iOS】——自定义UITableViewCell

2026-11-30 14:34:34 来源:互联网转载

文章目录

  • 一、实现方式
  • 二、实���代码

    一、实现方式

    我们可以通过继承 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 NS_ASSUME_NONNULL_BEGIN@interface myCustomCell : UITableViewCell@property (nonatomic, strong) UILabel *titleLabel;@property (nonatomic, strong) UILabel *subtitleLabel;@endNS_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 @interface ViewController : UIViewController{    //定义一个数据视图对象    //数据视图用来显示大量相同格式的信息的视图    //例如:电话通讯录、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

    运行结果如下:

上一篇:云服务器搭建加速器

下一篇:饥荒怎么开服务器好用些?