Skip to content

Commit

Permalink
性能优化/自定义cell改为直接注入类型,提升处理效率
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyouPrince committed Jun 26, 2022
1 parent 9f97b49 commit 489d3d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion SectionDemo/XYInfomationSection/XYInfomationCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef NS_ENUM(NSUInteger, XYInfoCellType) {
@interface XYInfomationItem : NSObject
/**
图片名 default is nil
@note 若使用此属性,应使用本地图片。若传入网络图地址,会从网络下载,比较耗时
*/
@property(nonatomic , copy) NSString *imageName;
/**
Expand Down Expand Up @@ -160,7 +161,7 @@ typedef NS_ENUM(NSUInteger, XYInfoCellType) {
@property (nonatomic, strong) UIFont *placeholderFont;

/// custom cell class - 当type == other 时候有效
@property (nonatomic, strong) NSString *customCellClass;
@property (nonatomic, strong) Class customCellClass;

/// cell 的指定 backgroundColor, default is clearColor
@property (nonatomic, strong) UIColor *backgroundColor;
Expand Down
17 changes: 13 additions & 4 deletions SectionDemo/XYInfomationSection/XYInfomationCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,21 @@ + (instancetype)cellWithModel:(XYInfomationItem *)model
break;
case XYInfoCellTypeOther: // 如果是自定义类型,那就根据model中自定义类来创建
{
if (NSClassFromString(model.customCellClass)) {
cell = [NSClassFromString(model.customCellClass) new];
}else
{
if ([model.customCellClass isKindOfClass:[NSNull class]]) {
@throw [[NSException alloc] initWithName:@"入参错误" reason:@"model.type为Other,必须传入 customCellClass" userInfo:nil];
}

// 兼容旧版本,v1.3之前定义 String 类型
if ([model.customCellClass isKindOfClass:[NSString class]]) {
NSString *className = (NSString *)model.customCellClass;
if (NSClassFromString(className)) {
cell = [NSClassFromString(className) new];
}
} else {
// 新版改为 Class 直接注册,提升性能
cell = [model.customCellClass new];
}

cell->_cell_type = XYInfoCellTypeOther;
cell.model = model;
}
Expand Down

0 comments on commit 489d3d4

Please sign in to comment.