QMUI_iOS
QMUI_iOS copied to clipboard
您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?
您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?比如业务需要根据后台去动态的添加,在点击的时候后就需要维护两次cellData的identifier,一次是对数据的,一次是对didSelectedRow方法,而且要跟数据源QMUIStaticTableViewCellData一一对应,比如现在索引是5,后来业务要求将这个索引改到4,那么在didSeletRow中也需要去再做一次判断,用NSInteger会不会局限性太大?
您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?比如业务需要根据后台去动态的添加,在点击的时候后就需要维护两次cellData的identifier,一次是对数据的,一次是对didSelectedRow方法,而且要跟数据源QMUIStaticTableViewCellData一一对应,比如现在索引是5,后来业务要求将这个索引改到4,那么在didSeletRow中也需要去再做一次判断,用NSInteger会不会局限性太大?
QMUIStaticTableViewCellData
中 identifier
的核心用处如下:
- (NSString *)reuseIdentifierForCellAtIndexPath:(NSIndexPath *)indexPath {
QMUIStaticTableViewCellData *data = [self cellDataAtIndexPath:indexPath];
return [NSString stringWithFormat:@"cell_%@", @(data.identifier)];
}
看得出来 identifier
最终其实还是要转成 String
来作为 cell
的 重用标识,因此你的需求就非常简单了,提供一种解决方案:
建一个分类:QMUIStaticTableViewCellData+QMUIStringIdentifier
@interface QMUIStaticTableViewCellData (QMUIStringIdentifier)
/// 提供 NSString 作为标识
/// 注意: 一旦 stringIdentifier 非空,则将无视 NSInteger 的 identifier
@property(nonatomic, copy) NSString *stringIdentifier;
@end
@interface QMUIStaticTableViewCellDataSource (QMUIStringIdentifier)
@end
@implementation QMUIStaticTableViewCellData (QMUIStringIdentifier)
QMUISynthesizeIdCopyProperty(stringIdentifier, setStringIdentifier)
@end
@implementation QMUIStaticTableViewCellDataSource (QMUIStringIdentifier)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
OverrideImplementation([QMUIStaticTableViewCellDataSource class], @selector(reuseIdentifierForCellAtIndexPath:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^NSString *(QMUIStaticTableViewCellDataSource *selfObject, NSIndexPath *firstArgv) {
QMUIStaticTableViewCellData *data = [selfObject cellDataAtIndexPath:firstArgv];
NSString *identifier = (data.stringIdentifier && data.stringIdentifier.length) ? data.stringIdentifier : [NSString stringWithFormat:@"%@", @(data.identifier)];
return [NSString stringWithFormat:@"cell_%@", identifier];
};
});
});
}
@end
您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?比如业务需要根据后台去动态的添加,在点击的时候后就需要维护两次cellData的identifier,一次是对数据的,一次是对didSelectedRow方法,而且要跟数据源QMUIStaticTableViewCellData一一对应,比如现在索引是5,后来业务要求将这个索引改到4,那么在didSeletRow中也需要去再做一次判断,用NSInteger会不会局限性太大?
QMUIStaticTableViewCellData
中identifier
的核心用处如下:- (NSString *)reuseIdentifierForCellAtIndexPath:(NSIndexPath *)indexPath { QMUIStaticTableViewCellData *data = [self cellDataAtIndexPath:indexPath]; return [NSString stringWithFormat:@"cell_%@", @(data.identifier)]; }
看得出来
identifier
最终其实还是要转成String
来作为cell
的 重用标识,因此你的需求就非常简单了,提供一种解决方案:建一个分类:
QMUIStaticTableViewCellData+QMUIStringIdentifier
@interface QMUIStaticTableViewCellData (QMUIStringIdentifier) /// 提供 NSString 作为标识 /// 注意: 一旦 stringIdentifier 非空,则将无视 NSInteger 的 identifier @property(nonatomic, copy) NSString *stringIdentifier; @end @interface QMUIStaticTableViewCellDataSource (QMUIStringIdentifier) @end
@implementation QMUIStaticTableViewCellData (QMUIStringIdentifier) QMUISynthesizeIdCopyProperty(stringIdentifier, setStringIdentifier) @end @implementation QMUIStaticTableViewCellDataSource (QMUIStringIdentifier) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ OverrideImplementation([QMUIStaticTableViewCellDataSource class], @selector(reuseIdentifierForCellAtIndexPath:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) { return ^NSString *(QMUIStaticTableViewCellDataSource *selfObject, NSIndexPath *firstArgv) { QMUIStaticTableViewCellData *data = [selfObject cellDataAtIndexPath:firstArgv]; NSString *identifier = (data.stringIdentifier && data.stringIdentifier.length) ? data.stringIdentifier : [NSString stringWithFormat:@"%@", @(data.identifier)]; return [NSString stringWithFormat:@"cell_%@", identifier]; }; }); }); } @end
@LYluyu 可以先这么处理,后续会考虑将 identifier 改为 id<NSCopy> 类型,因为这确实是个合理的建议。