0

I have one UITableView, two arrays and segmented control. When segmented control index is "1", UITableView display data from first array, when index is "2" - second array. I want to swipe to delete function only in second segment. How can I do that? I am thinking of some "if statements" in

tableView(tableView: UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 

but i don't know where to put them.

Ian
  • 12,538
  • 5
  • 43
  • 62
migari
  • 131
  • 2
  • 10

1 Answers1

1

If you use commitEditingstyle, then your cells will still display the Delete button when you swipe. Use editingStyleForRowAtIndexPath: instead. Put an if statement in to test which segment is selected, and return UITableViewCellEditingStyle.None (to disable swipe to delete) or .Delete (to enable swipe to delete) accordingly. If you want to be able to delete the cells by putting the table view into editing mode, then also test tableView.editing to determine whether to use .Delete or .None.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Thank You, great answer, but it works only if commitEditingstyle function is add as well. Only then I can swipe my finger and see delete. Why? It is working, but I don't understand how. – migari Dec 05 '14 at 23:08
  • Sorry, yes, you need to implement commitEditingStyle as well. The `editingStyleForRowAtIndexPath` is called by the table view when you swipe your finger, to determine whether it should display the Delete button or not. `commitEditingStyle` is only called when you press the delete button, so that you can implement the necessary code to remove the row. – pbasdf Dec 05 '14 at 23:44