RETableViewManager-FDTemplateLayoutCell 0.0.1

RETableViewManager-FDTemplateLayoutCell 0.0.1

测试测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年5月

mbpr13 维护。



 
依赖关系
REFormattedNumberField~> 1.1.5
REValidation~> 0.1.4
UITableView+FDTemplateLayoutCell~> 1.2
 

  • Lee Cheng-Yang

RETableViewManager

UITableView 的强大数据驱动内容管理器。

https://github.com/forkingdog/UITableView-FDTemplateLayoutCell
使用 RETableViewManager 与 UITableView-FDTemplateLayoutCell 实现自动调整单元格大小

自动高度单元格示例

#import "ExampleAutoHeightCell.h"

@implementation ExampleAutoHeightCell
@dynamic item;
-(void)cellDidLoad{
    [super cellDidLoad];
}
-(void)cellWillAppear{
    [super cellWillAppear];
    self.longStringLabel.text = self.item.longString;
}
//no need for + (CGFloat)heightWithItem:(RETableViewItem *)item tableViewManager:(RETableViewManager *)tableViewManager
@end


#import "DynamicHeightController.h"
#import <RETableViewManager.h>
#import "ExampleAutoHeightItem.h"
@interface DynamicHeightController ()
@property (nonatomic,strong) RETableViewManager *manager;
@end

@implementation DynamicHeightController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];
    self.manager[@"ExampleAutoHeightItem"]= @"ExampleAutoHeightCell";

    RETableViewSection *section = [RETableViewSection section];
    ExampleAutoHeightItem *item = [ExampleAutoHeightItem item];
    item.cellIdentifier = @"ExampleAutoHeightCell";
    item.longString = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    item.cellHeight = UITableViewAutomaticDimension;
    [section addItem:item];

    [self.manager addSection:section];

    [self.manager.tableView reloadData];
}
@end

RETableViewManager 允许轻松管理任何 UITableView 的内容,无论是表格还是列表。它基于可重用单元格技术,并提供将任何对象类映射到任何自定义子类的 API。

基本思想是允许开发者使用自己的 UITableViewUITableViewController 实例(甚至是子类),提供一个同步数据与单元格外观的层。它完全实现了 UITableViewDelegateUITableViewDataSource 协议,这样你就不必实现了。

RETableViewManager Screenshot

RETableViewManager Screenshot

RETableViewManager Screenshot

快速示例

在几行代码中将您的 UITableView 启动并运行

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create the manager and assign a UITableView
    //
    self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

    // Add a section
    //
    RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
    [self.manager addSection:section];

    // Add a string
    //
    [section addItem:@"Just a simple NSString"];

    // Add a basic cell with disclosure indicator
    //
    [section addItem:[RETableViewItem itemWithTitle:@"String cell" accessoryType:UITableViewCellAccessoryDisclosureIndicator selectionHandler:^(RETableViewItem *item) {
        NSLog(@"Test: %@", item);
    }]];

    // Custom items / cells
    //
    self.manager[@"CustomItem"] = @"CustomCell";

    [section addItem:[CustomItem item]];
}

RETableViewManager 预包装了可扩展且可生产使用的组件

  • 文本
  • 布尔值
  • 数字
  • 浮点数
  • 日期/时间
  • 长文本
  • 单选选项选择器
  • 多选选项选择器
  • 信用卡和到期日期

此外,RETableViewManager 还提供了用于超级简单单元格样式的 API。

要求

  • Xcode 5 或更高版本
  • Apple LLVM 编译器
  • iOS 6.0 或更高版本
  • ARC

演示

在 Xcode 中构建和运行 RETableViewManagerExample.xcworkspace 以查看 RETableViewManager 的实际应用。

安装

2) 包含源代码

将 RETableViewManager、REValidation、资源、REFormattedNumberField 文件夹包含在您的源代码中

API 快速入门

关键类
RETableViewManager 管理类。每个管理器都包含多个RETableViewSection部分。
RETableViewSection 代表RETableViewManager中的部分,每个部分包含多个RETableViewItem项。
RETableViewItem RETableViewItem是大多数项层次结构的根类。RETableViewManager通过
,项继承了一个与RETableViewItem通信的基本接口,并与RETableViewCellRETableViewManager.
RETableViewCell 类定义了在RETableViewCellUITableView对象中出现的单元格的属性和行为。您应该从子类化以获得满足您应用程序需求特定的单元格特性和行为。默认情况下,RETableViewCellRETableViewCellStylingRETableViewItem.
RETableViewCellStyle
为子类提供样式。您可以定义如 backgroundImageMarginRETableViewCellcellHeightcontentViewMargin, 等属性。, 辅助控制器RETableViewOptionsController
根据用户输入执行选择并在完成后提供结果。应与
RERadioItem 一起使用。RETableViewManager.

包含许多内置项和单元格,执行常见任务(文本输入、日期输入等)。

内置项和单元格
项目类 单元格类 描述
RETextItem RETableViewTextCell 提供用户文本输入的便捷性。您可以通过RETextItem设置一大堆属性,通常在UITextField.
中找到。 RELongTextItem RETableViewLongTextCell中找到。设置一大堆属性,通常在提供多行用户文本输入的便捷性。您可以通过.
UITextView 设置一大堆属性。 RENumberItem
RETableViewNumberCell 使用 REFormattedNumberField 提供用户数字输入的便捷性。 REBoolItemRETableViewBoolCell.
RETableViewManager RETableViewCell 使用 UISwitch 提供用户布尔输入的便捷性。RERadioItem.
REMultipleChoiceItem RETableViewCell 提供使用RERadioItem.
RETableViewOptionsController 选择单个选项的便捷性。 REFloatItem
RETableViewFloatCell 提供调整 0.0 到 1.0 范围内的浮点值的便捷性。 REDateTimeItemRETableViewDateTimeCell提供修改
NSDate 对象中日期的便捷性。 提供使用REPickerItem.
RETableViewPickerCell UIPickerView RESegmentedItemRETableViewSegmentedCell.
提供与 UISegmentedControl 的便捷性。

示例

创建部分示例

没有标题的部分

RETableViewSection *section = [RETableViewSection section];
[self.manager addSection:section];

带标题的部分

RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Header"];
[self.manager addSection:section];

带标题和脚注的部分

RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Header" footerTitle:@"Footer"];
[self.manager addSection:section];

带自定义头部视图的部分

RETableViewSection *section = [RETableViewSection sectionWithHeaderView:myCustomSectionHeaderView];
[self.manager addSection:section];

带有自定义头部和脚注视图的部分

RETableViewSection *section = [RETableViewSection sectionWithHeaderView:myCustomSectionHeaderView footerView:myCustomSectionFooterView];
[self.manager addSection:section];

文本(UITextField)和数字(REFormattedNumberField)项示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add items to the section
//
self.textItem = [RETextItem itemWithTitle:@"Enter text" value:@""];
[section addItem:self.textItem];

self.numberItem = [RENumberItem itemWithTitle:@"Enter text" value:@"" placeholder:@"(123) 456-7890" format:@"(XXX) XXX-XXXX"];
[section addItem:self.numberItem];

您可以在需要时随时读取 self.textItem.valueself.numberItem.value

布尔项(UISwitch)示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add a bool value cell (using UISwitch)
//
[section addItem:[REBoolItem itemWithTitle:@"Switch test" value:YES switchValueChangeHandler:^(REBoolItem *item) {
    NSLog(@"Value: %i", item.value);
}]];

单选按钮(RETableViewOptionsController)项示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add radio cell (options)
//

    __typeof (&*self) __weak weakSelf = self;

     RERadioItem *radioItem = [RERadioItem itemWithTitle:@"Radio" value:@"Option 4" selectionHandler:^(RERadioItem *item) {
        [item deselectRowAnimated:YES]; // same as [weakSelf.tableView deselectRowAtIndexPath:item.indexPath animated:YES];

        // Generate sample options
        //
        NSMutableArray *options = [[NSMutableArray alloc] init];
        for (NSInteger i = 1; i < 40; i++)
            [options addObject:[NSString stringWithFormat:@"Option %li", (long) i]];

        // Present options controller
        //
        RETableViewOptionsController *optionsController = [[RETableViewOptionsController alloc] initWithItem:item options:options multipleChoice:NO completionHandler:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];

            [item reloadRowWithAnimation:UITableViewRowAnimationNone]; // same as [weakSelf.tableView reloadRowsAtIndexPaths:@[item.indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];

        // Adjust styles
        //
        optionsController.delegate = weakSelf;
        optionsController.style = section.style;
        if (weakSelf.tableView.backgroundView == nil) {
            optionsController.tableView.backgroundColor = weakSelf.tableView.backgroundColor;
            optionsController.tableView.backgroundView = nil;
        }

        // Push the options controller
        //
        [weakSelf.navigationController pushViewController:optionsController animated:YES];
    }];

    [section addItem:radioItem];

结果

alt text

浮动项(UISlider)示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add a float item
//
[section addItem:[REFloatItem itemWithTitle:@"Float item" value:0.3 sliderValueChangeHandler:^(REFloatItem *item) {
    NSLog(@"Value: %f", item.value);
}]];

日期项目示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add a date item
//
[section addItem:[REDateTimeItem itemWithTitle:@"Date / Time" value:[NSDate date] placeholder:nil format:@"MM/dd/yyyy hh:mm a" datePickerMode:UIDatePickerModeDateAndTime]];

选择项示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add a picker item
//
[section addItem:[REPickerItem itemWithTitle:@"Picker" value:@[@"Item 12", @"Item 23"] placeholder:nil options:@[@[@"Item 11", @"Item 12", @"Item 13"], @[@"Item 21", @"Item 22", @"Item 23", @"Item 24"]]]];

分段项示例

// Create the manager
//
self.manager = [[RETableViewManager alloc] initWithTableView:self.tableView];

// Add a section
//
RETableViewSection *section = [RETableViewSection sectionWithHeaderTitle:@"Test"];
[self.manager addSection:section];

// Add a segmented item
//
[section addItem:[RESegmentedItem itemWithTitles:@[@"One", @"Two"] value:1 switchValueChangeHandler:^(RESegmentedItem *item) {
    NSLog(@"Value: %i", item.value);
}]];

验证

验证通过REValidation库进行。

示例

self.textItem = [RETextItem itemWithTitle:@"Text" value:@"" placeholder:@"Text item"];
self.textItem.validators = @[@"presence", @"length(3, 10)"];

self.emailItem = [RETextItem itemWithTitle:@"Email" value:@"" placeholder:@"Email item"];
self.emailItem.name = @"Your email";
self.emailItem.validators = @[@"presence", @"email"];

每个项目、每个部分和控制器都具有属性errors。此属性始终与每个级别的错误保持最新。例如,RETableViewItem将只具有它自己的验证错误,RETableViewSection将具有在该部分中发生的所有错误(每个项目一个)。RETableViewManager的属性errors将反映所有错误。

自定义单元格

RETableViewManager允许将自定义对象映射到自定义单元格。为了将您的自定义对象(项目)映射到单元格,只需编写

self.manager[@"CustomItem"] = @"CustomCell";

如果您查看RETableViewManager源代码,您可能会发现默认映射是如何执行的

- (void)registerDefaultClasses
{
    self[@"__NSCFConstantString"] = @"RETableViewCell";
    self[@"__NSCFString"] = @"RETableViewCell";
    self[@"NSString"] = @"RETableViewCell";
    self[@"RETableViewItem"] = @"RETableViewCell";
    self[@"RERadioItem"] = @"RETableViewOptionCell";
    self[@"REBoolItem"] = @"RETableViewBoolCell";
    self[@"RETextItem"] = @"RETableViewTextCell";
    self[@"RELongTextItem"] = @"RETableViewLongTextCell";
    self[@"RENumberItem"] = @"RETableViewNumberCell";
    self[@"REFloatItem"] = @"RETableViewFloatCell";
    self[@"REDateTimeItem"] = @"RETableViewDateTimeCell";
    self[@"RECreditCardItem"] = @"RETableViewCreditCardCell";
    self[@"REMultipleChoiceItem"] = @"RETableViewOptionCell";
}

您的自定义项目应从RETableViewItem继承。自定义单元格应从RETableViewCell继承。这是两个提供绑定您的子类所需所有必要逻辑的基础类。

在您的RETableViewCell子类中,您需要将一个item对象与其项目相关联。这可以通过声明它来完成

#import <RETableViewManager/RETableViewManager.h>
#import "CustomItem.h"

@interface CustomCell : RETableViewCell

@property (strong, readwrite, nonatomic) CustomItem *item;

@end

之后,您的自定义对象(项目)就可以在单元格中使用了。

RETableViewCell有3个基本方法需要实现

  • 类方法以调整单元格大小
+ (CGFloat)heightWithItem:(RETableViewItem *)item tableViewManager:(RETableViewManager *)tableViewManager;

将您的自定义项目传递到该方法以确定单元格大小。您需要返回计算出的尺寸。

  • 实例方法在创建单元格时触发。
- (void)cellDidLoad;

您可能会在这里创建单元格子视图。此方法仅调用一次,之后单元格将被复用。

  • 实例方法在每次单元格被复用时触发。
- (void)cellWillAppear;

cellWillAppear是给标签赋予值(从您的自定义项目)、调整颜色等的好地方。

快速示例

- (void)cellDidLoad
{
    [super cellDidLoad];
    self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    [self.contentView addSubview:self.testLabel];
}

- (void)cellWillAppear
{
    [super cellWillAppear];
    self.testLabel.text = self.item.someVariable;
}

界面构建支持

默认支持界面构建单元格,无需特殊设置。单元格和项目将被自动注册,就像RETableViewManager中的任何其他自定义单元格一样。

self.manager[@"XIBTestItem"] = @"XIBTestCell";

在这里,XIBTestItem将是您的单元格标识符,您应该在您的包中有一个XIBTestCell.xib文件。就是这样。

样式

使用RETableViewManager可以轻松自定义整个UITableView(或任何特定部分)的不同偏移和单元格背景图像。

RETableViewManagerRETableViewSection都有style属性(RETableViewCellStyle类的实例)。

这是一个关于自定义样式如何工作的快速示例

// Set default cell height
//
self.manager.style.cellHeight = 42.0;

// Set cell background image
//
[self.manager.style setBackgroundImage:[[UIImage imageNamed:@"First"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeFirst];
[self.manager.style setBackgroundImage:[[UIImage imageNamed:@"Middle"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeMiddle];
[self.manager.style setBackgroundImage:[[UIImage imageNamed:@"Last"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeLast];
[self.manager.style setBackgroundImage:[[UIImage imageNamed:@"Single"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeSingle];

// Set selected cell background image
//
[self.manager.style setSelectedBackgroundImage:[[UIImage imageNamed:@"First_Selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                                   forCellType:RETableViewCellTypeFirst];
[self.manager.style setSelectedBackgroundImage:[[UIImage imageNamed:@"Middle_Selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                                   forCellType:RETableViewCellTypeMiddle];
[self.manager.style setSelectedBackgroundImage:[[UIImage imageNamed:@"Last_Selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                                   forCellType:RETableViewCellTypeLast];
[self.manager.style setSelectedBackgroundImage:[[UIImage imageNamed:@"Single_Selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                                   forCellType:RETableViewCellTypeSingle];

self.manager.style.contentViewMargin = 10.0;
    self.manager.style.backgroundImageMargin = 10.0;

// Set a custom style for a particular section
//
self.accessoriesSection.style = [self.manager.style copy];
[self.accessoriesSection.style setBackgroundImage:[[UIImage imageNamed:@"First_Alt"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeFirst];
[self.accessoriesSection.style setBackgroundImage:[[UIImage imageNamed:@"Middle_Alt"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeMiddle];
[self.accessoriesSection.style setBackgroundImage:[[UIImage imageNamed:@"Last_Alt"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeLast];
[self.accessoriesSection.style setBackgroundImage:[[UIImage imageNamed:@"Single_Alt"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]
                           forCellType:RETableViewCellTypeSingle];

联系方式

罗马·叶菲莫夫

授权协议

RETableViewManager遵循MIT授权协议。

版权所有 © 2013 Roman Efimov。

特此免费授予任何获得本软件及其相关文档副本(“软件”)的个人以未经限制地使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或销售软件副本的权利,以及允许向本软件提供的人进行此类操作的权利,但需遵守以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和无侵犯专利权等保证。在任何情况下,作者或版权所有者不对因软件或软件的使用或其它操作而引起的任何索赔、损害或其他责任承担责任,无论其基于合同、侵权或其它行为。