JASwipeCell 1.0.1

JASwipeCell 1.0.1

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

JASwipeCell 维护。



概览

受 iOS 8 邮件启发。一个 UITableViewCell 子类,它显示可定制的左侧或右侧按钮,当用户以任一方向滑动单元格时,按钮将被揭示。最边缘的按钮将固定到容器视图,并执行类似于 iOS 8 邮件中的删除/存档按钮的事件。

功能

  • 支持在单元格的两侧添加可操作按钮。
  • 您可以自定义按钮的标题文本和颜色。
  • 每个按钮都有一个在按下时执行的手柄。
  • 当用户将单元格完全滑动时,最左侧或右侧的按钮将固定到顶部容器视图中。这将还将执行该按钮的操作。
  • 支持 iOS 7+

当您向左滑动时的按钮揭示示例

image

最右侧按钮固定到容器视图的示例

image

使用方法

设置单元格

第一步是子类化 JASwipeCell.h。您完全控制此单元格上渲染的视图,并且它们必须作为子视图添加到 JASwipeCell 的 topContainerView 中。您可以使用 xib 文件或在代码中执行此操作。提供的示例是使用 PureLayout 在代码中完成的。

设置按钮

下一步是设置左侧/右侧按钮。我创建了一个 JAActionButton 类,它有一个类方法可以快速创建带有标题、背景颜色和完成处理程序的按钮。

- (NSArray *)leftButtons
{
    __typeof(self) __weak weakSelf = self;
    JAActionButton *button1 = [JAActionButton actionButtonWithTitle:@"Delete" color:[UIColor redColor] handler:^(UIButton *actionButton, JASwipeCell*cell) {
        [cell completePinToTopViewAnimation];
        [weakSelf leftMostButtonSwipeCompleted:cell];
    }];

    JAActionButton *button2 = [JAActionButton actionButtonWithTitle:@"Mark as unread" color:kUnreadButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mark As Unread" message:@"Done!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];

    return @[button1, button2];
}

- (NSArray *)rightButtons
{
    __typeof(self) __weak weakSelf = self;
    JAActionButton *button1 = [JAActionButton actionButtonWithTitle:@"Archive" color:kArchiveButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        [cell completePinToTopViewAnimation];
        [weakSelf rightMostButtonSwipeCompleted:cell];
    }];

    JAActionButton *button2 = [JAActionButton actionButtonWithTitle:@"Flag" color:kFlagButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Flag" message:@"Flag pressed!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];
    JAActionButton *button3 = [JAActionButton actionButtonWithTitle:@"More" color:kMoreButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"More Options" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Option 1" otherButtonTitles:@"Option 2",nil];
        [sheet showInView:weakSelf.view];
    }];

    return @[button1, button2, button3];
}

设置表格视图。

现在您必须使用上面的按钮创建方法创建单元格实例。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JATableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kJATableViewCellReuseIdentifier];

    [cell addActionButtons:[self leftButtons] withButtonWidth:kJAButtonWidth withButtonPosition:JAButtonLocationLeft];
    [cell addActionButtons:[self rightButtons] withButtonWidth:kJAButtonWidth withButtonPosition:JAButtonLocationRight];

    cell.delegate = self;

    [cell configureCellWithTitle:self.tableData[indexPath.row]];
    [cell setNeedsLayout];
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;
}

响应用户代理方法:

当单元格上发生滑动事件时,有可用的用户代理方法列表将被调用。

@protocol JASwipeCellDelegate <NSObject>
- (void)leftMostButtonSwipeCompleted:(JASwipeCell *)cell;
- (void)rightMostButtonSwipeCompleted:(JASwipeCell *)cell;
@optional
- (void)swipingRightForCell:(JASwipeCell *)cell;
- (void)swipingLeftForCell:(JASwipeCell *)cell;
@end

在项目示例中,将单元格完全向左滑动将触发 "存档" 按钮执行。这将调用委托方法 rightMostButtonSwipeCompleted:,负责更新表格数据并删除行。

- (void)rightMostButtonSwipeCompleted:(JASwipeCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableData removeObjectAtIndex:indexPath.row];

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

示例项目

提供了一个示例项目。它需要iOS7,可以在设备或模拟器上运行。适用于任何设备尺寸。

创建者

Jose Alvarez

许可证

JASwipeCell是在MIT许可证下提供的。有关更多信息,请参阅LICENSE文件。