Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'didSelectRowAt' is not being called

i have a question about the UITableView delegate function didSelectRowAt. Everything is working fine but unfortunately the didSelectRowAt is not called. I read in some other stackoverflow question about the problem and tried some solutions out but none of them works for me. I made a subclass of the tableview which is the delegate himself:

class MyTableView : UITableView, UITableViewDelegate{

    override func awakeFromNib() {

        super.awakeFromNib()

        separatorStyle = .none

        backgroundView = nil
        backgroundColor = UIColor.clear

        isScrollEnabled = false

        delegate = self
        isEditing = false
        allowsSelection = true

    }


//    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
//       
//    this is working        
//
//    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print("not called")

    }    

} 

So all subclasses of MyTableView will implement the datasource stuff and this also working fine (in case somehow will mentioned this).

The strange thing is that didHighlightRowAt is called, so the delegate somehow works. Only the didSelectRowAt which I want is not being called.

By the way there aren't any UITapGestureRecognizer.

Can somehow give me any advice here. Are there properties which are wrong?

like image 472
Yetispapa Avatar asked Jul 07 '17 08:07

Yetispapa


People also ask

What is didSelectRowAt in Swift?

tableView(_:didSelectRowAt:)Tells the delegate a row is selected.

What is tableView delegate?

Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.

What is Uitableview in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

If you have a parent view with gesture recognizer assigned to it you'll need to do something like this:

self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: action)
if let tapGestureRecognizer = self.tapGestureRecognizer {
  tapGestureRecognizer.cancelsTouchesInView = false
  self.addGestureRecognizer(tapGestureRecognizer)
}

Once you told the gesture recognizer to stop canceling touches, your UITableView starts reacting on taps as normal

like image 54
igor Avatar answered Sep 18 '22 14:09

igor


Please check TableView's cell allowsSelection.

tableView.allowsSelection = true

Edit:

Check:

  • tableView.allowsSelection

  • cell.selectionStyle

  • User Interaction Enabled

like image 35
Lal Krishna Avatar answered Sep 18 '22 14:09

Lal Krishna