MTZTableViewManager 是一个强大的框架,允许您以描述性的方式创建表格视图,通过指定行和部分,无需担心索引。它还提供了一组用于创建表单和处理其输入、应用掩码、执行验证以及将复杂对象转换为其他对象和从其他对象转换为复杂对象的工具。
1.1.3
)。MTZTableViewManager.xcodeproj
文件拖放到您的 Xcode 项目中。MTZTableViewManager.framework
(位于 Products
下)拖放到您的目标 Linked Frameworks and Libraries
区域。开始相当简单。只需在某个地方声明一个自己的 MTZTableManager
。
@property (nonatomic) MTZTableManager *tableManager;
然后声明您的行、部分以及最终的数据。建议使用自定义的 UITableViewCell
子类。
MTZTableRow *row = [[MTZTableRow alloc] initWithClazz:[MyCustomCell class] action:^(NSIndexPath * _Nonnull indexPath, id<MTZModel> model) {
NSLog(@"Tap!");
}];
MTZTableSection *section = [[MTZTableSection alloc] initWithTableRows:@[row]];
MTZTableData *data = [[MTZTableData alloc] initWithTableSections:@[section]];
self.tableManager = [[MTZTableManager alloc] initWithTableView:self.tableView tableData:data];
然后您就可以开始了!请注意,当使用 MTZTableManager
时,您不能成为 tableView
的代理或数据源。
hidden
属性。nib
快速创建单元格。同样,您可以为每个部分提供自定义的头部和尾部类。要将对象声明为可能的模型,只需使其符合 MTZModel
。
@interface MyCustomCellModel: NSObject<MTZModel>
@property (nonatomic) NSString *text;
@end
单元格可以配置为显示信息。只需将您想要的单元格符合 MTZModelDisplaying
并实现所需的方法。
@interface MyCustomCell: UITableViewCell <MTZModelDisplaying>
@end
@implementation MyCustomCell
- (void)configureWithModel:(id<MTZModel>)model {
self.textLabel.text = ((MyCustomCellModel *)model).text;
}
@end
MTZModelDisplaying
,并且会被提供给自定义的头部/尾部(如果有的话)。表单对象是可以由 MTZTableViewManager
生成的表单操作的对象。理想情况下,您想使其所有属性都不可写,以避免外部修改,因为表单元素将通过 KVO
直接修改值。如果您正在使用 Swift,别忘了加上 dynamic
关键字!确保对象符合 MTZFormObject
。
@interface MyCustomFormObject: NSObject <MTZFormObject>
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSDate *date;
@property (nonatomic, readonly) MyCustomUser *user;
@end
为了使单元格与表单兼容,它需要符合 MTZFormEditing
并实现所需的方法。
@interface MyCustomTextFieldCell: UITableViewCell <MTZFormEditing>
@property (nonatomic) UITextField *textField;
@end
@implementation MyCustomTextFieldCell
// ...
- (UIControl<MTZFormField> *)fieldForFormObject {
return self.textField;
}
@end
MTZFormField
。框架已经为 UITextField
、UITextView
、UISwitch
和 UIStepper
提供了默认实现。表单字段可以自动为您提供 inputAccessoryView
,以在同一个部分内的其他字段之间进行跳转。要本地化框架提供的输入辅助视图中的按钮,只需在您的 Localizable.strings
中添加以下条目,并根据您的需求替换翻译
"mtz_prev" = "Prev";
"mtz_next" = "Next";
"mtz_done" = "Done";
日期是一个特殊的话题。因此,如果您想要与 NSDate
键路径交互,请使用 MTZTableFormDateRow
MTZTableFormDateRow *dateRow = [[MTZTableFormDateRow alloc] initWithClazz:[MyCustomTextFieldCell class] formObject:self.formObject keyPath:CLASSKEY(MyCustomFormObject, date)];
dateRow.minimumDate = [NSDate date];
dateRow.maximumDate = [[NSDate date] dateByAddingTimeInterval:60*60*24*15];
dateRow.datePickerMode = MTZDatePickerModeDateAndTime;
MTZTableFormDateRow
必须也将 UITextField
作为表单字段使用,因为我们用合适的选择器替换了 inputView
MTZDatePickerModeExpirationDate
作为选择器模式,并且它将使用 MTZExpirationDatePicker
作为 inputView
。如果您想要提供一组选项,请将您想要提供的对象类型 conforms 为 MTZFormOption
@interface MyCustomUser: NSObject <MTZFormOption>
@property (nonatomic) NSInteger ID;
@property (nonatomic) NSString *email;
- (instancetype)initWithID:(NSInteger)ID email:(NSString *)email;
@end
@implementation MyCustomUser
// ...
- (NSString *)optionDescription {
return self.email;
}
@end
然后,设置 MTZTableFormRow
上的 availableOptions
属性
NSArray *allUsers = @[[[MyCustomUser alloc] initWithID:1 email:@"[email protected]"],
[[MyCustomUser alloc] initWithID:2 email:@"[email protected]"],
[[MyCustomUser alloc] initWithID:3 email:@"[email protected]"]];
MTZTableFormRow *userRow = [[MTZTableFormRow alloc] initWithClazz:[MyCustomTextFieldCell class] formObject:self.formObject keyPath:CLASSKEY(MyCustomFormObject, user)];
userRow.availableOptions = allUsers;
UITextField
作为表单字段,因为我们用合适的选择器替换了 inputView
TBA
TBA
TBA
TBA
MTZTableViewManager
在 MIT 许可下发布。有关详细信息,请参阅 LICENSE。