DBPickableItemCell 0.1.0

DBPickableItemCell 0.1.0

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
发布最后发布2015年1月

Daniel Bowden维护。




一个 UITableViewCell 子类,它呈现 UIPickerView,用于从多个对象中进行选择。支持 iPhone(通过 inputView)和 iPad(通过 UIPopoverController)。任何符合 DBPickableItem 协议的对象都可以作为 pickerview 中的选项。

这可以简化从表格或菜单中的项目列表中进行选择。

包含了示例项目。克隆仓库并打开 DBPickableItemCellDemo.xcworkspace

使用方法

可以创建 DBPickableItemCell 的子类,或者像示例项目中的那样直接在您的 UITableView 中使用(ViewController.h/m)。

希望列在可选择的列表中的任何对象都必须符合 DBPickableItem 协议。为此,必须实现以下方法

- (BOOL)isEqualToPickableItem:(id<DBPickableItem>)item;
- (NSString *)displayString;

例如,Person 对象可能是如下所示的

//Person.h
@interface Person : NSObject <DBPickableItem>

-displayString 方法用于返回列表中单个选择项应显示的内容。

//Person.m
- (NSString *)displayString
{
  return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}

-isEqualToPickableItem: 方法用于确保列表呈现时可以选择当前选中的项。此方法必须实现,尽管您可以自由决定两个项目相等的标准。例如,这可以是对应的用户ID、名称等。如示例项目中的 Person.m 和 Country.m 展示的那样

呈现可选择的列表

要从您的表格视图中呈现可选择的列表,您可以在 -tableView:didSelectRowAtIndexPath: 中调用 -showPickableItems:currentlySelectedItem:selectionBlock: 方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DBPickableItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DBPickableItemCellIdentifier" forIndexPath:indexPath];

  __weak typeof(self)weakSelf = self;

  [cell showPickableItems:self.people currentlySelectedItem:self.currentPerson selectionBlock:^(id<DBPickableItem> item) {
    weakSelf.currentPerson = item;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadData];
  }];
}

iPhone

iPhone

iPad

iPad

安装

DBPickableItemCell 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 中

pod "DBPickableItemCell"

作者

Daniel Bowden

github.com/danielbowden

twitter.com/danielgbowden

许可

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