ITable - UITableView 的重要替代和补充

ITable 对 UITableView 的重要补充和替代, 大部分的页面功能(View Controller)都是直接继承 ITable. ITable 支持 UITableView 的数据与展现分离的特性, 同时支持 Static Cells 特性. ITable 使用 addDataSource() 类似的方法添加要进行显示的数据和视图, 这是一种自然而然的逻辑, 而不是使用 Delegate 那样逆思维的方式.

API

@interface ITable : UIViewController

@property (nonatomic, readonly) UIScrollView *scrollView;

@property (nonatomic, readonly) IPullRefresh *pullRefresh;
@property (nonatomic) IRefreshControl *headerRefreshControl;
@property (nonatomic) IRefreshControl *footerRefreshControl;

@property (nonatomic) IView *headerView;
@property (nonatomic) IView *footerView;

- (void)clear;
- (void)reload;

- (void)registerViewClass:(Class)ivClass forTag:(NSString *)tag;

- (void)addIViewRow:(IView *)view;
- (void)addIViewRow:(IView *)view defaultHeight:(CGFloat)height;
- (void)addDataRow:(id)data forTag:(NSString *)tag;
- (void)addDataRow:(id)data forTag:(NSString *)tag defaultHeight:(CGFloat)height;

- (void)prependIViewRow:(IView *)view;
- (void)prependIViewRow:(IView *)view defaultHeight:(CGFloat)height;
- (void)prependDataRow:(id)data forTag:(NSString *)tag;
- (void)prependDataRow:(id)data forTag:(NSString *)tag defaultHeight:(CGFloat)height;

- (void)addDivider:(NSString *)css;
- (void)addDivider:(NSString *)css height:(CGFloat)height;

- (void)onHighlight:(IView *)view atIndex:(NSUInteger)index;
- (void)onUnhighlight:(IView *)view atIndex:(NSUInteger)index;
- (void)onClick:(IView *)view atIndex:(NSUInteger)index;

- (void)onRefresh:(IRefreshControl *)refreshControl state:(IRefreshState)state;
/**
 * Must call this method in onRefresh() when state is IRefreshBegin
 */
- (void)endRefresh:(IRefreshControl *)refreshControl;

@end

Demo

// just like UITableCell
@interface ListItemView : IView
@end

@implementation ListItemView
-(id)init {
    self = [super init];
    {
        IView *sub = [[IView alloc] init];
        [sub.style set:@"float: center; valign: middle; width: 100; height: 50; background: #3cf;"];
        [self addSubview:sub];
    }
    [self.style set:@"height: 100; border-bottom: 1 solid #333;"];
    return self;
}
// get called when is about to show data
-(void)setData:(id)data {
    [super setData:data];
    NSString *s = (NSString *)data;
    if([s isEqual:@"on"]){
        // highlight with yellow color
        [self.style set:@"background: #ff3"];
    }else{
        // grey
        [self.style set:@"background: #eee"];
    }
}
@end



@interface ITableController ()

@end

@implementation ITableController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Test";

    [self registerViewClass:[ListItemView class] forTag:@"item"];
    
    // get data from network
    [self addDataRow:@"on" forTag:@"item"];
    [self addDataRow:@"on" forTag:@"item"];
    [self addDataRow:@"off" forTag:@"item"];
    [self addDataRow:@"on" forTag:@"item"];
    [self reload];
}