Reputation: 2296
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?
Upvotes: 25
Views: 18848
Reputation: 1
I had the same issue. A tableview, embedded in a container view that, in turn was collapsable (via a UIPanGestureRecognizer).
Turned out that, in addition to tap.cancelsTouchesInView = false
and the delegate's shouldRecognizeSimultaneouslyWith
, also had to set tap.delaysTouchesBegan = true
.
Apparently, some sort of race condition between all the recognizer's around.
Upvotes: 0
Reputation: 2589
check you didSelectRowAt
method may be you have typo didDeselectRowAtIndexPath
There is a chance you accidentally typed didDeselectRowAtIndexPath
Upvotes: 1
Reputation: 2045
Objective C - Possible Solutions:
You just have to use:
tap.cancelsTouchesInView = NO;
Code:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(yourFunctionOnTap)];
tap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap];
tableView.allowsSelection = true
Upvotes: 0
Reputation: 398
For me some views was blocking each other and because i've set tableView:shouldHighlightRowAt:
to false
i wasn't able to detect the problem
Upvotes: 1
Reputation: 2381
There might be the following issue s:
self.tableview.delegate = self
self.tableview.isUserInteractionEnabled = true
self.tableview.allowsSelection = true
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Upvotes: 10
Reputation: 4765
I added a mechanism to hide keyboard when tapping out of text field:
...
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap(recognizer:)))
tapRecognizer.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapRecognizer)
}
@objc func handleSingleTap(recognizer: UITapGestureRecognizer) {
self.view.endEditing(true)
}
The handleSingleTap
stole the event. I removed everything above, and used this instead:
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
view.endEditing(true)
}
And editing came back!
Upvotes: 3
Reputation: 937
For me, changing selection style, from none, solved the problem.
Upvotes: 22
Reputation: 41
I suggest writing replace this on your code.
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
}
Upvotes: 4
Reputation: 2035
If your ViewController
has UIGestureRecognizer
You should disable UIGestureRecognizer.cancelsTouchesInView
. For example:
extension UIViewController {
public func hideKeyboarOnTap() {
let tap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboardAction))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
}
@objc private func hideKeyboardAction() {
self.view.endEditing(true)
}
}
Upvotes: 12
Reputation: 761
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
Upvotes: 50
Reputation: 193
I the same problem, even though the delegate was setup correctly. In my case, it was not possible to interact with the table view because it was not the fist subview from the user's perspective. You can make sure it is displayed in front by using this method when you add it to your main view:
view.insertSubview(yourTableView, at: self.view.subviews.count)
or bring it to the front using this:
self.view.bringSubview(toFront: yourTableView)
Upvotes: 1
Reputation: 16160
Please check TableView's
cell allowsSelection.
tableView.allowsSelection = true
Edit:
tableView.allowsSelection
cell.selectionStyle
User Interaction Enabled
Upvotes: 37
Reputation: 619
I dont know what others solutions you tried, but if you are using a custom viewcell , it might be that you didint enable "User Interaction Enabled" on that component.
Upvotes: 2