iOS控件-TypeTableView

接口部分

@protocol UITableViewTypeDataSource <NSObject>

@optional

-(NSString *_Nullable)tableView:(UITableView *_Nonnull)tableView sectionTypeInSection:(long)section;

-(NSString *_Nullable)tableView:(UITableView *_Nonnull)tableView rowTypeAtIndexPath:(NSIndexPath *_Nonnull)indexPath;


@end


@interface TypeTableView : UITableView

@property (nonatomic, weak) id<UITableViewTypeDataSource> typeDataSource;

-(NSString *_Nullable)getTypeWithSection:(long)section;

-(NSString *_Nullable)getTypeWithIndexPath:(NSIndexPath *)indexPath;

@end

实现部分

@interface SGTypeTableView ()

@property (nonatomic, strong) NSMutableDictionary *sectionTypes;
@property (nonatomic, strong) NSMutableDictionary *rowTypes;

@end

@implementation SGTypeTableView

- (NSMutableDictionary *)sectionTypes
{
    if (_sectionTypes == nil) {
        _sectionTypes = [NSMutableDictionary dictionary];
    }
    return _sectionTypes;
}
-(NSMutableDictionary *)rowTypes
{
    if (_rowTypes == nil) {
        _rowTypes = [NSMutableDictionary dictionary];
    }
    return _rowTypes;
}

-(void)reloadData
{
    [self loadSectionAndRowTypes];
    [super reloadData];
}

-(void)loadSectionAndRowTypes
{
    [self.sectionTypes removeAllObjects];
    [self.rowTypes removeAllObjects];

    long sectionNumber = 0;
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sectionNumber = [self.dataSource numberOfSectionsInTableView:self];
    }
    
    for (long section = 0; section < sectionNumber; section ++) {
        if (self.typeDataSource && [self.typeDataSource respondsToSelector:@selector(tableView:sectionTypeInSection:)]) {
            NSString *sectionType = [self.typeDataSource tableView:self sectionTypeInSection:section];
            if (sectionType) {
                NSString *sectionKey = [NSString stringWithFormat:@"%ld",section];
                self.sectionTypes[sectionKey] = sectionType;
            }
        }
        
        long rowNumber = 0;
        if (self.dataSource && [self.dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
            rowNumber = [self.dataSource tableView:self numberOfRowsInSection:section];
        }
        NSMutableDictionary *rowTypesDic = [NSMutableDictionary dictionary];
        for (long row = 0; row < rowNumber; row ++) {
            if (self.typeDataSource && [self.typeDataSource respondsToSelector:@selector(tableView:rowTypeAtIndexPath:)]) {
                NSString *rowType = [self.typeDataSource tableView:self rowTypeAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
                if (rowType) {
                    NSString *rowKey = [NSString stringWithFormat:@"%ld",row];
                    rowTypesDic[rowKey] = rowType;
                }
            }
        }
        if (rowTypesDic.count > 0) {
            NSString *sectionKey = [NSString stringWithFormat:@"%ld",section];
            self.rowTypes[sectionKey] = rowTypesDic;
        }
    }
}
-(NSString *)getTypeWithSection:(long)section
{
    NSString *sectionKey = [NSString stringWithFormat:@"%ld",section];
    NSString *sectionType = self.sectionTypes[sectionKey];
    return sectionType;
}
-(NSString *)getTypeWithIndexPath:(NSIndexPath *)indexPath
{
    NSString *sectionKey = [NSString stringWithFormat:@"%ld",indexPath.section];
    NSDictionary *rowTypesDic = self.rowTypes[sectionKey];
    if (rowTypesDic.count > 0) {
        NSString *rowKey = [NSString stringWithFormat:@"%ld",indexPath.row];
        NSString *rowType = rowTypesDic[rowKey];
        return rowType;
    }
    return nil;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容