MOOSelectableListViewController
是一个 UITableViewControler
子类,简化了对特定列表中项目的选择管理。
以下是一个基本示例:
//Build a list of dummy values, each item should be an NSDictionary with the values 'title' and 'value'
NSMutableArray *items = [NSMutableArray new];
for (int i = 0; i < 10; i ++)
[items addObject:@{ @"title" : @"Item Title", @"value" : @(i) }];
//initalise the view controller (using basic settings), see readme or header for more advanced options
MOOSelectableListViewController *rootViewController = [[MOOSelectableListViewController alloc] initWithItems:items changedBlock:^(id title, id value) {
NSLog(@"Selected item: %@ (%@)",title, value);
}];
[self presentViewController:rootViewController animated:YES completion:nil];
传递给视图控制器的项目数组必须包含包含 title
和 value
键的 NSDictionary
对象。title
应该是一个 NSString
,而 value
可以是任何 NSObject
,但它必须是唯一的。
- (id)initWithItems:(NSArray *)items changedBlock:(didSelectItemBlock)block selectedItem:(id)selected cellClass:(Class)cellClass tableViewStyle:(UITableViewStyle)style;
您还可以指定以下信息以进行更高级的使用
UITableViewStyle
(默认为 UITableViewStylePlain
)UITableViewCell
类(必须是 MOOSelectableListCell
的子类)selectedItem
的值 isEqual:
相等的项将被标记为选中MOOSelectableListCell
是 UITableViewCell
的基本子类,对其进行子类化非常简单。
您的自定义子类应实现 updateCellWithTitle:value:selected:
方法。该方法在每次调用 tableView:cellForRowAtIndexPath:
时都会被调用。
布尔值 selected
用于指定是否 [value isEqualTo:selectedValue]。
此属性用于指定是否应更新 selectedValue
属性。它默认为 YES
,但当设置为 NO
时,单元格将不会重新加载以显示选择,只会调用 didSelectItemBlock
以指示已选择项目。
如果您指定了 selectedItem
,则应将此属性保留为 YES
。
iOS 5.0 + 使用 ARC
参见 LICENSE