didSelectRowAtIndexPath: not being called

前端 未结 14 2181
轻奢々
轻奢々 2020-11-29 18:57

I have a UITableView as a subview of my UIScrollVIew, which is the main view controlled by my MainViewController.

In MainView

相关标签:
14条回答
  • 2020-11-29 19:10

    Maybe it is a typo after all. Check that your function is not didDeselectRowAtIndexPath: (de select instead of select).

    0 讨论(0)
  • 2020-11-29 19:10

    I was having this issue for a while, and I did not see any reference to it here, so for reference, another reason for this could be that:

    tableView.editing = YES;
    

    but

    tableView.allowsSelectionDuringEditing = NO;
    

    As per documentation for

    - tableView:didSelectRowAtIndexPath:
    

    This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode). See "Managing Selections" in Table View Programming Guide for iOS for further information (and code examples) related to this method.

    0 讨论(0)
  • 2020-11-29 19:12

    Updated for Swift 3:

    if you are used UITapGestureRecognizer in your code :- # Swift 3 use below lines of code:

    extension YourViewController{
        func hideKeyboardWhenTappedAround() {
            let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.dismissKeyboard))
            view.addGestureRecognizer(tap)
            tap.cancelsTouchesInView = false
        }
    
        func dismissKeyboard() {
            view.endEditing(true)
        }
    }
    

    How to called:- In ViewDidLoad()

    self.hideKeyboardWhenTappedAround()
    
    0 讨论(0)
  • 2020-11-29 19:19

    I had intermittent failure of didSelectRowAtIndexPath: being called on my custom cell press.

    I discovered that if I stopped calling [tableView reloadData] very often (10 Hz), and changed it to update every 2 seconds, almost every press would successfully call didSelectRowAtIndexPath:

    It seems like reloading the view blocks presses.

    0 讨论(0)
  • 2020-11-29 19:21

    A cell can be selected by the user (tapping on the row), by calling "tableView.selectRowAtIndexPath(..)" or "cell.setSelected(true, ...).

    • If the cell is selected by calling "cell.setSelected(true)", the user cannot deselect the cell anymore.

    • If the cell is selected by calling "tableView.selectRowAtIndexPath()", the user can deselect the cell as expected.

    0 讨论(0)
  • 2020-11-29 19:23

    Your problem is case-sensitivity. Your code:

    - (void)tableVIew:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
    

    should be

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
    
    0 讨论(0)
提交回复
热议问题