BMXSwipableCell 1.2.6

BMXSwipableCell 1.2.6

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

Massimiliano Bigatti 维护。



  • Massimiliano Bigatti

一个自定义的 UITableViewCell,支持滑动手势以显示按钮或其他视图的自定义菜单。它支持

  • 完全可定制的 UIView,用作自定义按钮和其他视图的画布;
  • 自动布局;
  • 故事板;
  • 简单的单元格重用;
  • 单元格的高亮和非高亮;
  • 单元格的选择和取消选择;
  • 普通和编辑模式;
  • 访问视图;
  • 兼容 iOS 7 和 8。

BMXSwipableCell 尽可能地模仿 iOS7 邮件应用的原始行为。

image

BMXSwipableCell 对故事板友好,因为它不自己实现单元格内容,而是使用 Interface Builder 中定义的元素。当然,也可以通过代码实现内容。

image

tableView:cellForRowAtIndexPath 中,BMXSwipableCell 在swiper时应显示什么内容时是通用的。它可以是两个按钮、一个按钮或完全不同的内容。因此,BMXSwipableCell 不自己实现“更多”和“删除”按钮,而是导出了一个“基础”视图,即接口构建器中定义的原生内容视图之下的视图。

安装

手动

BMXSwipableCellDemo/BMXSwipableCell 文件夹拖放到您的项目,并将其添加到您的目标中。

使用

  1. 如果您使用的是 Interface Builder,将单元格的 Custom Class 设为 BMXSwipableCell。如果您根据代码操作,请使用 UITableView 注册 BMXSwipableCell
  2. 实现 tableView:cellForRowAtIndexPath 方法,并按需配置单元格。这里通过类别完成
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    BMXSwipableCell *cell = (BMXSwipableCell *)
        [tableView dequeueReusableCellWithIdentifier: @"Cell"
                                        forIndexPath: indexPath];

    [cell configureCellForItem: [_data objectAtIndex: indexPath.row]];

    return cell;
} 

在将子视图添加到基础视图之前,您可以通过属性 basementConfigured 检查单元格是否已初始化(可以重复使用单元格可以已经有自定义的基础内容)

@implementation BMXSwipableCell (ConfigureCell)

- (void)configureCellForItem:(BMXDataItem*)item
{
    CGFloat cellHeight = CGRectGetHeight(self.bounds);
    CGFloat x = self.basementVisibleWidth - cellHeight * 2;

    //
    // configure cell only if not already done
    //
    if (!self.basementConfigured) {    
        // first button...

        //
        // delete button
        //
        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteButton.backgroundColor = 
            [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0f];
        deleteButton.frame = CGRectMake(x + cellHeight, 0, cellHeight, cellHeight);
        [deleteButton setTitle:@"Delete" forState:UIControlStateNormal];
        [deleteButton setTitleColor:[UIColor whiteColor] 
                            forState: UIControlStateNormal];
        [deleteButton addTarget: self
                         action: @selector(userPressedDeleteButton:)
               forControlEvents: UIControlEventTouchUpInside];

        [self.basementView addSubview: deleteButton];
    }

    // configure cell contents

}
//... more
  1. 向您的 UITableViewController / UITableViewDelegate 添加一些方法来管理设备旋转或列表滚动时的自动隐藏基础视图
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                duration:(NSTimeInterval)duration 
{

    [super willRotateToInterfaceOrientation: toInterfaceOrientation
                                   duration: duration];

    [BMXSwipableCell coverBasementOfAllCells];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [BMXSwipableCell coverBasementOfAllCells];
}

此代码也可以在 UITableViewController+BMXSwipableCellSupport 类别中找到,以方便您使用。

查看示例项目以获得完整使用示例。

关于 Interface Builder 的注释

  1. 请记住将 自定义类 设置为 BMXSwipableCell
  2. 为了始终获得正确的背景颜色(正常和编辑模式都适用),请在单元格和内容视图中都配置 背景 属性。

致谢

联系方式

http://bigatti.it
@mbigatti