多种Cell表格的一种写法

背景介绍

  • 一个表格,有多种cell,这种页面越来越常见

  • 取得cell的代理函数,会有一大串的if比较

  • 确定cell高度的代理函数,会有一大串的if比较

一种方法

  • 利用Object-C的动态特性,主要是Class _Nullable NSClassFromString(NSString *aClassName)

  • 将cell的类名放在数据源中,可以提供一个父类,命名好类名的属性。

  • 利用继承,提供一个cell的父类,命名好需要用到的函数,在具体的子类中实现。

  • 使用的时候,就可以用Class _Nullable NSClassFromString(NSString *aClassName),避免大量的if判断。

例子代码

BaseModel.h

#import <Foundation/Foundation.h>

@interface BaseModel : NSObject

// 具体调用的cell的类名
@property (nonatomic, strong) NSString *className;

@end

BaseTableViewCell.h

#import <UIKit/UIKit.h>
#import "BaseModel.h"

@interface BaseTableViewCell : UITableViewCell

// cell生成函数
+ (instancetype)cellWithTableView:(UITableView *)tableView;

// 更新表格内容
- (void)updateCellWithModel:(BaseModel*)model;

// 表格高度
+ (CGFloat)rowHeight:(BaseModel*)model;

@end

BaseTableViewCell.m

#import "BaseTableViewCell.h"

#define kDefaultCellHeight                 44

@implementation BaseTableViewCell

#pragma mark - interface

// 这样写可以隐藏reuseIdentifier字符串
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *reuseIdentifier = @"BaseTableViewCell";
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (void)updateCellWithModel:(BaseModel *)model {
    if (nil == model) {
        return;
    }
    
    // 做其他内容更新操作
    
    // 重新autolayout
    [self setNeedsUpdateConstraints];
}

+ (CGFloat)rowHeight:(HHDiscoveryHomeModel *)model {
    return kDefaultCellHeight;
}

#pragma mark - life cycle

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 创建UI元素,设置属性,并加到cell的contentView
    }
    return self;
}

@end

xxxController.m

#import "BaseTableViewCell.h"

@interface xxxController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray <BaseModel *> *modelArray;

@end

@implementation xxxController    // 具体的使用者

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseModel *model = self.modelArray[indexPath.row];
    Class classFromName = NSClassFromString(model.className);
    BaseTableViewCell *cell = [classFromName cellWithTableView:tableView];
    [cell updateCellWithModel:model];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseModel *model = self.modelArray[indexPath.row];
    Class cell = NSClassFromString(model.className);
    return [cell rowHeight:model];
}

@end
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容