Andrew Walz Andrew Walz - 2 months ago 37
Swift Question

Not responding gestures UITableVIew editActionsForRow

I am currently trying to add edit functionality to the tableView inside a View Controller. This view controller is added on top of a parent ViewController which is a MapView (Mapbox).

Currently, I can scroll the tableView up and down with Vertical scrolling without any issue. However, the horizontal swipe to bring up actions on a TableViewCell results in the mapview scrolling its position. I cannot find a while to ignore the scrolling gestures of the mapview. Disabling scrolling of the map also does not fix the issue. TableView Editing code:

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]
}


And the function that adds the UIViewController containing the TableView on top of the Mapview:

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
}

Answer

Ok, I have pieced together a few different answers which combined seemed to solve my problem.

First:

Ensure your editing delegates are configured - note the editingStyleForRow differences

- (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;
}

Second:

Ensure your gestures are not intercepted. I added this category to my UIViewController hosting my UITableView, and I also added the shouldRecognizeSimultaneouslyWithGestureRecognizer delegate method to my UITableViewCell implementation (can be added by category if using a default UITableViewCell similar to the UITableView category below)

@interface UITableView (CellSwipeAdditions)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end

@implementation UITableView (CellSwipeAdditions)
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
@end