我想模仿滑动删除UITableViewCell的功能,就像iOS 8中的邮件应用程序一样 . 我不是指滑动以显示删除按钮 . 我指的是当你刷卡时,它会破坏3个动作,但是如果你继续向左滑动,那么电子邮件就会被删除 .
在iOS 8中,UITableView有一个新方法,您可以在其中提供数据以显示任意数量的按钮:
#ifdef __IPHONE_8_0
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *viewStackRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Stack" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"View Stack Action");
}];
viewStackRowAction.backgroundColor = [UIColor radiusBlueColor];
UITableViewRowAction *viewUserRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"User" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"View User Action");
}];
viewUserRowAction.backgroundColor = [UIColor radiusLightBlueColor];
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"Delete");
}];
deleteRowAction.backgroundColor = [UIColor redColor];
return @[deleteRowAction, viewUserRowAction, viewStackRowAction];
}
#endif
我没有看到任何API来检测你是否继续刷卡 . 我在UITableView.h中使用了8_0,上面的方法似乎是唯一的新方法 .
我想可以监视滚动视图偏移,或添加/劫持UIPanGestureRecognizer . 我只是想确保使用默认方式,如果有的话(并获得“免费”动画)
4 回答
使用Swift 4.2和iOS 12,您可以根据需要选择 3 following ways 中的一个,以创建将删除所选
UITableViewCell
的拖尾滑动操作 .#1 . 使用UITableViewDataSource的tableView(_:commit:forRowAt :)
当
tableView(_:commit:forRowAt:)
与editingStyle
的值UITableViewCell.EditingStyle.delete
一起使用时,系统会自动支持完全滑动以删除 .#2 . 使用UITableViewDelegate的tableView(_:editActionsForRowAt :)和UITableViewRowAction
为了支持使用
UITableViewRowAction
完全滑动删除,您必须使用值为UITableViewRowAction.Style.destructive
的style
对其进行初始化 .#3 . 使用UITableViewDelegate的tableView(_:trailingSwipeActionsConfigurationForRowAt :)和UISwipeActionsConfiguration(需要iOS 11)
UISwipeActionsConfiguration
有一个名为performsFirstActionWithFullSwipe的属性 .performsFirstActionWithFullSwipe
有以下声明:以下
UITableViewController
实现显示如何使用UISwipeActionsConfiguration
来管理完全滑动以删除操作 .将ui gustere识别器添加到每个单元格中,检查“swipness”的数量,如果超过特定阈值,则执行删除操作 .
像这样的东西:
您的表视图的数据源必须实现
否则内置的iOS 8滑动功能将无法正常工作 .
这似乎违反直觉,因为
UITableViewRowAction
接受了一个块 . 但是_2468503已经能够让它发挥作用 .你可以使用MGSwipeTableCell . 他们已经实现了这个功能来触发回调swipeTableCell:tappedButtonAtIndex:direction:fromExpansion:tappedButtonAtIndex等于0(因此它会执行你在第一次添加的按钮上实现的内容) .