Ios UITableViewCell“;全称;从右向左滑动
我有一个UITableView,单元格在从右向左滑动时已经实现了两个自定义按钮(UITableViewRowAction编辑操作)。一切都很好 我想在用户“一路”滑动(也是从右向左)时触发其中一个按钮的动作,类似于Mail.app 代码为Swift 3,适用于iOS 9和iOS 10 我有我认为足够的代码Ios UITableViewCell“;全称;从右向左滑动,ios,uitableview,swipe,Ios,Uitableview,Swipe,我有一个UITableView,单元格在从右向左滑动时已经实现了两个自定义按钮(UITableViewRowAction编辑操作)。一切都很好 我想在用户“一路”滑动(也是从右向左)时触发其中一个按钮的动作,类似于Mail.app 代码为Swift 3,适用于iOS 9和iOS 10 我有我认为足够的代码 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITabl
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
print("commit editingStyle")
}
commit editingStyle
函数从未启动,正如我所期望的那样
我删除了我的editActionsForRowAt
方法,认为在定义自定义按钮时可能弄错了什么。我现在看到了默认的“删除”按钮,但是commit editingStyle
仍然不会启动
值得一提的是,这是UIViewController(不是UITableViewController)中的UITableView,其中.dataSource和.delegate都设置为所讨论的类。它也是一个NSFetchedResultsControllerDelegate。将一个
UISwipegestureRecognitizer
附加到表本身,而不是单个单元格。当检测到刷卡时,计算哪个单元格被刷卡,并相应地调用您的刷卡操作。添加以下内容:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
我提出了一种机制来实现这一点,我认为这不是一个太多的黑客行为——当然比我找到的其他解决方案要少。欢迎评论 我的目的始终是尽可能多地使用标准功能。我所做的是跟踪用户向左滑动手机主体的距离。当达到某个阈值时,我会告诉UITableView为所讨论的按钮执行从EditActionsErrorwat调用的相同操作 我使用-225作为单元格向左移动多远的阈值。这可能不适用于所有的实现(我可能会通过更多的测试来修改自己)。在我的例子中,我有两个自定义操作,其中一个按钮有点宽 我唯一真正添加的是NSTimer,我使用.UITrackingRunLoopMode将其添加到主运行循环中。这样做的好处是,当(并且仅当)用户移动手指时才运行。当计时器启动时,我检查单元格相对于主机tableView的位置。没有添加视图,没有添加手势识别器 在自定义UITableViewCell中,我添加了以下内容:
var trackTimer:Timer? = nil
weak var tableView:StudentsLessonsViewController? = nil
override func awakeFromNib() {
super.awakeFromNib()
setupTimer()
}
override func prepareForReuse() {
super.prepareForReuse()
setupTimer()
}
func setupTimer() {
self.trackTimer = Timer.init(timeInterval: 0.1, target: self, selector: #selector(performActionIfScrolledFarEnough), userInfo: nil, repeats: true)
if let trackTimer = self.trackTimer {
RunLoop.main.add(trackTimer, forMode: .UITrackingRunLoopMode)
}
}
func performActionIfScrolledFarEnough() {
let globalPoint = self.superview?.convert(self.frame.origin, to: self.tableView?.tableView)
//print("globalPoint.x: \(String(describing: globalPoint?.x))")
if let globalPoint = globalPoint {
if globalPoint.x < CGFloat(-225) {
//print ("Perform Action NOW!!!")
trackTimer?.invalidate()
if let tableView = self.tableView {
tableView.primaryActionButtonPressed(cell: self)
}
}
}
}
我的UITableView中已经有了这个函数,当用户点击EditActionsFrrowat中定义的一个按钮时,我调用了这个函数:
func tableView.primaryActionButtonPressed(cell:UITableViewCell)
在primaryActionButtonPressed函数中,我将单元格从tableView中删除(概念上类似于删除)。不同的操作可能需要不同的实施细节。已经准备好了-我应该在我最初的问题中包括这一点。正在按预期调用此函数。(无论值多少钱,一次刷卡就要打三次电话。)我想了想,但感觉像是我宁愿避免的那种黑客行为。我相信
commit editingStyle
就是为了这个目的而存在的,所以我希望它能起作用。值得一提的是,即使是主/详细模板项目(Xcode 8.3.2)也有这些方法,不支持完全滑动。也许我正在读的东西,暗示从右到左的完全滑动是内置的,是不准确的。
func tableView.primaryActionButtonPressed(cell:UITableViewCell)