我目前正在尝试向 View Controller 内的 tableView 添加编辑功能。
此 View Controller 添加在父 ViewController 之上,它是 MapView (Mapbox)。
目前,我可以通过垂直滚动向上和向下滚动 tableView,没有任何问题。
但是,水平滑动以在 TableViewCell 上显示操作会导致 mapview 滚动其位置。
我不能花点时间忽略 map View 的滚动手势。
禁用 map 滚动也不能解决问题。 TableView 编辑代码:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let test1 = UITableViewRowAction(style: .normal, title: "Test1") { (_, indexPath) in
}
test1 = UIColor=.red
let test2 = UITableViewRowAction(style: .normal, title: "Test2") { (_, indexPath) in
}
test2 = UIColor.blue
return [test1, test2]
}
以及在 Mapview 之上添加包含 TableView 的 UIViewController 的函数:
self.detailView = UIView(frame: CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: self.view.bounds.height))
self.view.addSubview(self.detailView!)
self.detailViewController = TestViewController()
self.detailViewController!.view.frame = self.view.bounds
self.detailView?.addSubview(self.detailViewController!.view)
self.addChildViewController(self.detailViewController!)
UIView.animate(withDuration: 1.0) {
self.detailView?.frame = self.view.bounds
}
最佳答案
好的,我拼凑了几个不同的答案,这些答案结合起来似乎可以解决我的问题。
首先:
确保您的编辑委托(delegate)已配置 - 注意 editingStyleForRow 的差异
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// handle your data source changes here
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// This is important, as the editing behavior changes the editing style for some reason?
return tableView.isEditing ? UITableViewCellEditingStyleNone: UITableViewCellEditingStyleDelete;
}
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
第二个:
确保您的手势不被拦截。我将这个类别添加到托管 UITableView 的 UIViewController 中:
@interface UIView (CellSwipeAdditions)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end
@implementation UIView (CellSwipeAdditions)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end
关于ios - 不响应手势 UITableVIew editActionsForRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39902052/