如何写静态tableView页面

学习了这篇文章 架构篇-谈谈如何写个人中心这类页面(静态tableView页面的编写)
给自己的学习做个记录。
根据自己的理解重写了一部分代码。


  • WynStaticCell.h
#import "WynBaseCell.h"

@class BaseCellDescriber;
@interface WynBaseStaticCell : WynBaseCell

@property (nonatomic, strong) BaseCellDescriber *describer;

@end


static NSString * const kUITableViewCell = @"WynBaseStaticCell";
static NSString * const kBaseCell = @"WynBaseStaticCell";

typedef void (^CustomCellBlock)(WynBaseStaticCell *cell, BaseCellDescriber *describer);
@interface BaseCellDescriber : NSObject
/**
 判断Cell 由代码还是xib创建 */
@property (nonatomic, assign) CellCreationType cellCreationType;
@property (nonatomic, copy) NSString *identifier;
/**
 UITableView default value */
@property (nonatomic, copy) NSString *textLabel_text;
@property (nonatomic, strong) UIFont *textLabel_font;
@property (nonatomic, strong) UIColor *textLabel_textColor;

@property (nonatomic, copy) NSString *detailTextLabel_text;
@property (nonatomic, strong) UIFont *detailTextLabel_font;
@property (nonatomic, strong) UIColor *detailTextLabel_textColor;

@property (nonatomic, assign) UITableViewCellAccessoryType accessoryType;

/**
 Block */
// (可选)在block中对cell赋值
@property (nonatomic, copy) CustomCellBlock customCellBlock;
@property (nonatomic, copy) void(^selectCellBlock)(WynBaseStaticCell *cell, BaseCellDescriber *describer);

- (CGFloat)cellHeight;
- (CustomCellBlock)defaultCustomCellBlock;

@end
  • WynStaticCell.m
#import "WynStaticCell.h"

static const CGFloat defaultCellHeight = 44.0;
@implementation WynBaseStaticCell


#pragma mark - Life cycle.
/*
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    }

    return self;
} */

- (CGSize)sizeThatFits:(CGSize)size {
    return CGSizeMake(0, defaultCellHeight);
}

- (void)setDescriber:(BaseCellDescriber *)describer {
    if (describer.identifier == kBaseCell) {
        self.textLabel.text = describer.textLabel_text;
        self.textLabel.font = describer.textLabel_font;
        self.textLabel.textColor = describer.textLabel_textColor;

        self.detailTextLabel.text = describer.detailTextLabel_text;
        self.detailTextLabel.font = describer.detailTextLabel_font;
        self.detailTextLabel.textColor = describer.detailTextLabel_textColor;

        self.accessoryType = describer.accessoryType;
    }
}
@end

#pragma mark -

@interface BaseCellDescriber ()

@property (nonatomic, weak) WynBaseStaticCell *cell;

@end

@implementation BaseCellDescriber

- (instancetype)init {
    if (self = [super init]) {
        _customCellBlock = [self defaultCustomCellBlock];
    }

    return self;
}

- (CustomCellBlock)defaultCustomCellBlock {
    __weak typeof(self) wkSelf = self;
    return ^(WynBaseStaticCell *cell, BaseCellDescriber *describer) {
        __strong typeof(self) sgSelf = wkSelf;
        sgSelf.cell = cell;
        [cell setDescriber:describer];
    };
}

- (CGFloat)cellHeight {
    return [_cell sizeThatFits:CGSizeZero].height;
}

@end

  • WynStaticTableView.h
#import 
#import "WynStaticCell.h"

@interface WynStaticTableView : UITableView

@property (nonatomic, assign) CGFloat headerHeight;

/**
 添加Describer

 @param block block
 @param section 需要添加到的section
 */
- (void)addCellDescriber:(BaseCellDescriber *(^)())block inSection:(NSInteger)section;

/**
 一次性添加多个describers eg. @[@[describer1, describer2], @[describers3]]

 @param describers describers
 */
- (void)loadCellDescribers:(NSArray *> *)describers;
/**
 注册cell

 @param nibCells 存放 通过xib创建的cell类名 的数组
 @param classCells 存放 通过代码创建的cell类名 的数组
 */
- (void)registNibCells:(NSArray *)nibCells classCells:(NSArray *)classCells;

@end
  • WynStaticTableView.m
#import "WynStaticTableView.h"

@interface WynStaticTableView() <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray<NSArray *> *cellDescriptionDatas;

@end

@implementation WynStaticTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;

        _headerHeight = 0.1f;
    }
    return self;
}

- (void)addCellDescriber:(BaseCellDescriber *(^)())block inSection:(NSInteger)section {
    BaseCellDescriber *describer = block();

    if(describer.cellCreationType == CellCreationType_code) {
        [self registerClass:[NSClassFromString(describer.identifier) class] forCellReuseIdentifier:describer.identifier];
    } else if (describer.cellCreationType == CellCreationType_xib) {
        [self registerNib:[UINib nibWithNibName:describer.identifier bundle:nil] forCellReuseIdentifier:describer.identifier];
    }

    [[self cellDescriptionDatasAtIndexCheck:section] addObject:describer];
}
- (NSMutableArray *)cellDescriptionDatasAtIndexCheck:(NSInteger)index {
    if (index == [self.cellDescriptionDatas count]) {
        NSMutableArray *arrM = [NSMutableArray array];
        [self.cellDescriptionDatas addObject:arrM];
        return arrM;
    }

    id value = [self.cellDescriptionDatas objectAtIndex:index];
    if (value == [NSNull null]) {
        NSMutableArray *arrM = [NSMutableArray array];
        [self.cellDescriptionDatas addObject:arrM];
        return arrM;
    }
    return value;
}

- (void)loadCellDescribers:(NSArray<NSArray *> *)describers {
    _cellDescriptionDatas = [NSArray arrayWithArray:describers];
}

- (void)registNibCells:(NSArray<NSString *> *)nibCells classCells:(NSArray<NSString *> *)classCells {
    if (nibCells != nil) {
        for (NSString *cellString in nibCells) {
            // 注册 xib创建cell
            [self registerNib:[UINib nibWithNibName:cellString bundle:nil] forCellReuseIdentifier:cellString];
        }
    }
    if (classCells != nil) {
        for (NSString *cellString in classCells) {
            // 注册 代码创建cell
            [self registerClass:[NSClassFromString(cellString) class] forCellReuseIdentifier:cellString];
        }
    }
}


#pragma mark - UITableViewDataSource.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _cellDescriptionDatas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cellDescriptionDatas[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseCellDescriber *data = _cellDescriptionDatas[indexPath.section][indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:data.identifier];
    data.customCellBlock((WynBaseStaticCell *)cell, data);
    return cell;
}


#pragma mark - UITableViewDelegate.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return _headerHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseCellDescriber *data = _cellDescriptionDatas[indexPath.section][indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:data.identifier];
    data.customCellBlock((WynBaseStaticCell *)cell, data);
    return [data cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BaseCellDescriber *data = _cellDescriptionDatas[indexPath.section][indexPath.row];
    if (data.selectCellBlock) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        data.selectCellBlock((WynBaseStaticCell *)cell, data);
    }
}


@end

使用例子

  1. e.g.
   - (void)viewDidLoad {
    [super viewDidLoad];
 WynStaticTableView *tableView = [[WynStaticTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.backgroundColor = [UIColor lightTextColor];

    tableView.headerHeight = 20.f;

    ......

    BaseCellDescriber *cell0 = [BaseCellDescriber new];
    cell0.identifier = kBaseCell;
    cell0.textLabel_text = @"UITableViewCell";
    cell0.textLabel_textColor = [UIColor brownColor];
    cell0.detailTextLabel_text = @"detailTextLabel_text";
    cell0.detailTextLabel_textColor = [UIColor brownColor];
    cell0.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell0.selectCellBlock = ^(WynBaseStaticCell *cell, BaseCellDescriber *describer) {
        NSLog(@"*=*=%s=*=*", __func__);
    };

    [tableView loadCellDescribers:@[@[cell2Data, cell0], @[cell3Data]]];
        [tableView registNibCells:nil classCells:@[ kUITableViewCell]];

    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

}
  1. e.g.
    [tableView addCellDescriber:^BaseCellDescriber *{
        MCDemoTableDescribeData *cell3Data = [[MCDemoTableDescribeData alloc] init];
        cell3Data.identifier = @"MCDemoCell3";
        cell3Data.indicateImageName = @"indicate";
        cell3Data.title = @"cell3";
        cell3Data.subTitle = @"cell3's subtitle";

        return cell3Data;
    } inSection:0];

    [tableView addCellDescriber:^BaseCellDescriber *{
        BaseCellDescriber *cell0 = [BaseCellDescriber new];
        cell0.identifier = kBaseCell;
        cell0.textLabel_text = @"UITableViewCell";
        cell0.textLabel_textColor = [UIColor brownColor];
        cell0.detailTextLabel_text = @"detailTextLabel_text";
        cell0.detailTextLabel_textColor = [UIColor brownColor];
        cell0.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell0.selectCellBlock = ^(WynBaseStaticCell *cell, BaseCellDescriber *describer) {
            NSLog(@"*=*=%s=*=*", __func__);
        };
        return cell0;
    } inSection:1];

推荐用第二种吧。

你可能感兴趣的:(objective-c)