WSTableKit 0.9.2

WSTableKit 0.9.2

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最后发布2016年5月

Alex Nikishin 维护。



  • 作者
  • Alex Nikishin

WSTableKit 是一个简单框架,提供一种基于块的来管理您的 UITableViewUITableViewController 的方法。它是创建静态或基于 API 的表的不错解决方案。

主要特性

  • 忘掉代理模式,把您的表看作它本身 - 数据(行)的集合。
  • 与特定数据实体(行)关联的基于块的 action。
  • 内置针对顶部/底部单元格分隔符的解决方案,可以进行良好的自定义。
  • WSTableKit 支持静态高度单元格以及 Autolayout 单元格。

要求

  • iOS 7.0

安装

用法

简单用例

从某些数据数组的表

创建简单表所需的所有操作是创建一个填充有 WSCellItem 单元格的 WSTableSection 部分,并将其设置为 tableView 代理和数据源。您可以使用部分的 adjustmentBlock 来填充您的单元格视图以及进行其他调整,但建议将此代码封装在您的单元格类中(见下一个示例)。

// Some controller

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) WSTableSection *section;

...

- (void)viewDidLoad {
    [super viewDidLoad];

    // Your array of entities to show
    NSArray *data = @[@"One", @"Two", @"Three"]; 
    // Array of items that represents table cells
    NSArray *cells = [WSCellItem cellItemsWithClass:[WSTableViewCell class] objects:data]; 
    // Create section of cells with adjustment block that makes our data visible in cell.
    self.section = [WSTableSection sectionWithItems:cells adjustmentBlock:^(WSTableViewCell *cell, WSCellItem *item, NSIndexPath *path) {
        cell.textLabel.text = item.object;
    }]; 
    // Make section ruler of your table
    self.tableView.dataSource = self.section;
    self.tableView.delegate = self.section;
}

实现自定义单元格或子类化 WSTableViewCell

您可以使用提供的实体填充您自定义/子类化的单元格,或者通过实现 WSCellClass 协议方法 -(void)applyItem:(WSCellItem *)item 进行一些额外的单元格调整。

// Custom cell
@implementation CustomTableViewCell

- (void)applyItem:(WSCellItem *)item {
    self.textLabel.text = item.object;
}

...

// Controller
NSArray *data = @[@"One", @"Two", @"Three"]; 
NSArray *cells = [WSCellItem cellItemsWithClass:[CustomTableViewCell class] objects:data]; 
self.section = [WSTableSection sectionWithItems:cells];

选择处理

要处理选择事件,请使用 selectionBlock 设置或初始化您的单元格项。您可以为特定单元格进行选择控制,只需添加到只有在单元格需要响应用户选择事件时才必要的单元格中即可。您可以在任何时刻通过为项目分配新的 selectiobBlock 来改变选择行为。

// Per item selectionBlock
[WSCellItem itemWithCellClass:[WSTableViewCell class] object:@"One" selectionBlock:^(BOOL selected, WSCellItem *item, NSIndexPath *path) {
    if (selected) {
        // select
    } else {
        // deselect
    }
}];

// Multiple items selectionBlock
NSArray *cells = [WSCellItem cellItemsWithClass:[WSTableViewCell class] objects:data selectionBlock:^(BOOL selected, WSCellItem *item, NSIndexPath *path) {
    // code here
}];

带有自定义 actions 的单元格

您的一些表格单元格可能具有生成事件(按钮点击、文本输入等)的能力,并且您应该适当地响应它们。《WSCellItem》提供了一种基于块的动作机制,这有助于您处理此类事件。

// Custom cell
static NSString* const WSButtonClickedActionKey = @"WSButtonClickedActionKey";

@interface CustomTableViewCell : UITableViewCell ()
@property (nonatomic, strong) WSCellItem *item; // keep cell item

...

@implementation CustomTableViewCell

- (IBAction)buttonClicked:(id)sender {
    [self.item invokeActionForKey:WSButtonClickedActionKey withCell:self]; // Invoke action for key if it available
}

...

// Controller 

// Create action
WSCellAction *action = [WSCellAction actionWithKey:WSButtonClickedActionKey shortActionBlock:^(WSTableViewCell *cell) {
    NSLog(@"Button pressed");
}];

// Create another one with different action block.
WSCellAction *otherActionForSameKey = [WSCellAction actionWithKey:WSButtonClickedActionKey actionBlock:^id(WSTableViewCell *cell, NSDictionary *userInfo) {
    NSLog(@"Button pressed");
    return @(YES); // Success
];

// Init item with action or add it later 
WSCellItem *item = [WSCellItem itemWithCellClass:[CustomTableViewCell class] object:@"One" customAction:action];
[item addAction:otherActionForSameKey]; // CAUTION: cell item can have only one action per key and it will override previous one

请注意,WSCellAction可能包含不同的动作块,即WSCellActionShortBlock(无返回值)和WSCellActionBlock(返回值作为对事件的响应)。正如您所猜测的,每个动作实例可以有不同的无返回值块,但只有一个有返回值的行为块。有关WSCellAction的更多信息,请参见深入理解块。

WSTableKit深入理解

WSCellItem

@protocol WSCellClass <NSObject>

+ (NSString *)cellIdentifier;
- (void)applyItem:(WSCellItem *)item;

@optional
// WSTableKit use default UITableView rowHeight value in case of this method has not implemented by your cell. CAUTION: WSTableViewCell has default implementation that returns system default 44pt row height. 
- (CGFloat)heightWithItem:(WSCellItem *)item;

@end

@interface WSCellItem : NSObject

@property (nonatomic, assign, readonly) Class<WSCellClass> cellClass;
@property (nonatomic, strong, readonly) id object;
@property (nonatomic, copy) WSCellSelectionBlock selectionBlock;
@property (nonatomic, copy) WSCellAdjustmentBlock adjustmentBlock;

WSCellItem表示表格中的单行。任何单元格都必须知道它的表示单元格类,这是使单元格准备好使用所需的一个唯一字段。但WSCellItem在此之上还有很多附加功能,如单元格动作、选择事件处理或最终单元格调整。

您的单元格类必须遵守简单的WSCellClass协议,但建议使用WSTableViewCell类或继承它,因为它提供了协议方法的默认实现。

进行中

作者

Alex Nikishin

许可证

WSTableKit可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。