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