QMUI_iOS icon indicating copy to clipboard operation
QMUI_iOS copied to clipboard

您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?

Open LYluyu opened this issue 4 years ago • 2 comments

您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?比如业务需要根据后台去动态的添加,在点击的时候后就需要维护两次cellData的identifier,一次是对数据的,一次是对didSelectedRow方法,而且要跟数据源QMUIStaticTableViewCellData一一对应,比如现在索引是5,后来业务要求将这个索引改到4,那么在didSeletRow中也需要去再做一次判断,用NSInteger会不会局限性太大?

LYluyu avatar Oct 23 '20 11:10 LYluyu

您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?比如业务需要根据后台去动态的添加,在点击的时候后就需要维护两次cellData的identifier,一次是对数据的,一次是对didSelectedRow方法,而且要跟数据源QMUIStaticTableViewCellData一一对应,比如现在索引是5,后来业务要求将这个索引改到4,那么在didSeletRow中也需要去再做一次判断,用NSInteger会不会局限性太大?

QMUIStaticTableViewCellDataidentifier 的核心用处如下:

- (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

codingiran avatar Nov 20 '20 03:11 codingiran

您好,QMUIStaticTableViewCellData中identifier是否可以是字符串?比如业务需要根据后台去动态的添加,在点击的时候后就需要维护两次cellData的identifier,一次是对数据的,一次是对didSelectedRow方法,而且要跟数据源QMUIStaticTableViewCellData一一对应,比如现在索引是5,后来业务要求将这个索引改到4,那么在didSeletRow中也需要去再做一次判断,用NSInteger会不会局限性太大?

QMUIStaticTableViewCellDataidentifier 的核心用处如下:

- (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> 类型,因为这确实是个合理的建议。

MoLice avatar Nov 20 '20 16:11 MoLice