tableView(_:didSelectRowAt:) still active when in edit mode

I have the "didSelectRowAt" method in my swift app so that when I tap a cell the AVPlayer plays a video associated with the cell. I'm currently trying set the table up so that the user can go into edit mode, select multiple cells, and share the videos via my UIActivityViewController. My issue is that when the table's in edit mode and I select the selection circle, it plays the video. Now, according the SD (https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614877-tableview) this shouldn't happen.


Here's my select function

func select(){
        if (self.tableView.isEditing == false){
          
            self.tableView.setEditing(true, animated: true)
        }
      
        else{
      
            self.tableView.setEditing(false, animated: true)
          
        }
    }


and my cell selection method

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
        let index = tableView.indexPathForSelectedRow
        let videoStrings = self.videoSTRINGS
        let indNum = index!.row
        let videoNum = videoStrings[indNum]
        let video = URL(string: videoNum)
        self.avPlar = AVPlayer(url: video!)
        self.avPlarCon.player = avPlar
        self.present(self.avPlarCon, animated: true){
            self.avPlarCon.player!.play()
        }
       
    }



Any help is much apprecciated.

Accepted Reply

When reading documentation, we should not go here if it is true.

The only reason I could imagine would be that you call

func selectRow(at indexPath: IndexPath?, animated: Bool, scrollPosition: UITableViewScrollPosition)

elsewhere in code.

But even this should not call didselectrow if in editing mode!

If not, I would file a bug on this specific point: how can the delegate func be called when isEditing is true.

Replies

Just tested it, and it is still active during edit mode. Could it be because of how I use the didSelectedRowAt method? The way it works is it plays the same video in the doc dir that the cell is associated with. It doesn't play the video in the cell, but directly from the cell's source.





Is Piperade kinda like a chili? But with peppers?

So, I reformulate again, to make sure I understand correctly.


it is still active during edit mode

The log inside func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) prints ?


Could you check the value of self.tableView.isEditing

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("TableView in editing mode ", tableView.isEditing)
        let index = tableView.indexPathForSelectedRow


should always be false if it prints.


PS: piperade = onion, tomato, pimento, garlic…

The log prints text when I hit the select button. I also made the select button print the bool of isEditing, and it was cycling between true and false. I'm going to try your suggestion this evening.



PS: my mouth starting watering at onion, garlic and tomato.

It prints true. What are the possibilities of this being a bug?

When reading documentation, we should not go here if it is true.

The only reason I could imagine would be that you call

func selectRow(at indexPath: IndexPath?, animated: Bool, scrollPosition: UITableViewScrollPosition)

elsewhere in code.

But even this should not call didselectrow if in editing mode!

If not, I would file a bug on this specific point: how can the delegate func be called when isEditing is true.

Nope. I typed in "select" in the search field and found nothing on that. I'm going to file a bug report. In the meantime do you have any idea on a workaround?

As a workaround, I would try this:


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.isEditing {    //  Swift.print("Waiting for bug solution")          
               return
          }
        let index = tableView.indexPathForSelectedRow
        let videoStrings = self.videoSTRINGS
        let indNum = index!.row
        let videoNum = videoStrings[indNum]
        let video = URL(string: videoNum)
        self.avPlar = AVPlayer(url: video!)
        self.avPlarCon.player = avPlar
        self.present(self.avPlarCon, animated: true){
            self.avPlarCon.player!.play()
        }
       
    }


Good continuation.

Oh my............. that worked.........IT'S WORKING!!! Wow. I really want to thank you Claude for all the help and time you've given me. I hope one day that I'll be able to do it in person.🙂

Great !!!! And so simple, but just a patch.


You should file the bug anyway.


And thanks for the feedback (not everyone does !)

Not a problem! You saved my **** today! And yeah, I'm working on reporting that bug.