美文网首页
iOS控件-TypeTableView

iOS控件-TypeTableView

作者: 和女神经常玩 | 来源:发表于2022-12-30 18:12 被阅读0次

接口部分

@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

相关文章

网友评论

      本文标题:iOS控件-TypeTableView

      本文链接:https://www.haomeiwen.com/subject/qqeqqdtx.html