我想模仿 UITableViewCell 的滑动删除功能,就像 iOS 8 中的邮件应用程序一样。我指的不是滑动显示删除按钮。我指的是当你滑动时,它会显示 3 个 Action ,但如果你一直向左滑动,电子邮件就会被删除。
在 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 中 grep 了 8_0,上面的方法似乎是唯一的新方法。
我想可以监视 ScrollView 偏移,或添加/劫持 UIPanGestureRecognizer。我只是想确保使用默认方式,如果有的话(并“免费”获得动画)
最佳答案
使用 Swift 4.2 和 iOS 12,根据您的需要,您可以选择以下 3 种方式中的一种来创建将删除所选 UITableViewCell的拖尾滑动操作
.
#1。使用 UITableViewDataSource
的 tableView(_:commit:forRowAt:)
当您使用 tableView(_:commit:forRowAt:)
时,editingStyle
的值为 UITableViewCell.EditingStyle.delete
,完全滑动到系统自动支持删除。
import UIKit
class TableViewController: UITableViewController {
var numbers = [Int](0..<10)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numbers.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(numbers[indexPath.row])"
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCell.EditingStyle.delete) {
self.numbers.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
}
#2。使用 UITableViewDelegate
的 tableView(_:editActionsForRowAt:)
和 UITableViewRowAction
为了支持使用 UITableViewRowAction
完全滑动删除,您必须使用值为 UITableViewRowAction.Style.destructive< 的
.style
对其进行初始化
import UIKit
class TableViewController: UITableViewController {
var numbers = [Int](0..<10)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numbers.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(numbers[indexPath.row])"
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// Intentionally blank in order to be able to use UITableViewRowActions
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteHandler: (UITableViewRowAction, IndexPath) -> Void = { _, indexPath in
self.numbers.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
let deleteAction = UITableViewRowAction(style: UITableViewRowAction.Style.destructive, title: "Delete", handler: deleteHandler)
// Add more actions here if required
return [deleteAction]
}
}
#3。使用 UITableViewDelegate
的 tableView(_:trailingSwipeActionsConfigurationForRowAt:)
和 UISwipeActionsConfiguration
(需要 iOS 11)
UISwipeActionsConfiguration
有一个名为 performsFirstActionWithFullSwipe
的属性. performsFirstActionWithFullSwipe
具有以下声明:
var performsFirstActionWithFullSwipe: Bool { get set }
A Boolean value indicating whether a full swipe automatically performs the first action. [...] When this property is set to
true
, a full swipe in the row performs the first action listed in theactions
property. The default value of this property istrue
.
以下 UITableViewController
实现展示了如何使用 UISwipeActionsConfiguration
来管理完全滑动以删除操作。
import UIKit
class TableViewController: UITableViewController {
var numbers = [Int](0..<10)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numbers.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(numbers[indexPath.row])"
return cell
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let handler: UIContextualAction.Handler = { (action: UIContextualAction, view: UIView, completionHandler: ((Bool) -> Void)) in
self.numbers.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
}
let deleteAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: "Delete", handler: handler)
// Add more actions here if required
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = true
return configuration
}
}
关于ios - 全刷UITableViewCell删除UITableView iOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26002751/