UITableView didSelectRowAt is not called iOS 10 but works for 9.3.5
There are many questions regarding this issue, but the answers I have found so far are not applicable. In this case, the table works correctly in iOS 9.3.5, but not for iOS 10 (or 10.3.1)
I excluded:
- Wrong delegate setup. Delegate 3 functions,
didSelectRowAt
,heightForRowAt
andeditActionsForRowAt
. The latter is right-to-left scrolling, which allows for the same functionality as row selection. These three functions work correctly on v9.3.5 (as tested with a simulator and an older iPad I am testing with). When I use v10 with a simulator or my iPhone SE,heightForRowAt
andeditActionsForRowAt
but notdidSelectRowAt
(and for that matter - I triedwillSelectRowAt
. It also didn't work for v10.) - Any typographical error (as it works for 9.3.5)
I haven't been able to find anything about Swift changeover relative tableView(:didSelectRowAt)
to iOS 9 to iOS 10.
Background
I have a view controller with an appropriate xib ("DetailVC"). This xib has a label and then a table below the label. The table cell is another xib.
The view controller is exposed via
let uomVC = DetailVC()
self.navigationController?.pushViewController(uomVC, animated: true)
Then in DetailVC I use NSFetchedDataController and connect it to tableview via DataView Data Source delegate. (The master data is loaded and displayed perfectly.)
Code (I will send additional code as needed / requested - Swift 3)
viewDidLoad () - partial
let nib: String = "DetailCell"
let reuseID: String = "detailCell"
tableView.register(UINib.init(nibName: nib, bundle: nil), forCellReuseIdentifier: reuseID)
Tableview (: cellForRowAt)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseID) as! IngredientDetailCell
cell.present(ingredient: _fetchedResultsController.object(at: indexPath))
cell.isUserInteractionEnabled = true // Added later as a guess per SO suggestions
return cell
}
For arguments - this won't print
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected a row")
// delegate?.selectedIngredient(_fetchedResultsController.object(at: indexPath))
// self.navigationController?.popViewController(animated: true)
}
View
source to share
For completeness, if anyone has such a weird situation.
The problem was my UITableViewCell xib. I had to design it initially as a UIView and then added a UITableViewCell. The hierarchy must start at UITableViewCell. I moved the UITableViewCell out from under the view (outlined in red), removed the view, and reconnected the IBOutlets. The weird thing is that it worked with iOS 9.3.5.
source to share