19

I have a custom UITableViewCell subclass which contains a few IBAction methods wired up from a nib.

Under iOS 8 everything works as expected. If the user taps one of the buttons, the appropriate IBAction is called. If the user taps anywhere else on the cell, the UITableView didSelectRowAtIndexPath is called.

Under iOS 9, my IBAction methods are never called. The UITableView didSelectRowAtIndexPath is always fired regardless of where the user taps in the cell (button or not).

The project is compiled under Xcode 6 / Swift 1.2.

I found this post on an Apple forum w/ no solution. They seem to have the same issue.

https://forums.developer.apple.com/thread/16179

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
Chad Pavliska
  • 1,233
  • 12
  • 17

2 Answers2

29

I figured out the solution, hopefully this answer will save someone else the time.

I inherited this code base and it had some issues. The UITableViewCell's that were not working under iOS 9 were not using UITableViewCell nibs w/ a ContentView. Instead, someone had created a UIView nib and then pointed it to a custom UITableViewCell subclass. The nibs did not have a ContentView and apparently iOS 9 is a little more picky about that. This implementation worked fine under iOS 8 and there were no compiler or runtime errors or warnings.

The solution was to create a new UITableViewCell subclass and let Xcode auto create the nib for it. Then I copied over all of the UI and code from the old nib and pasted it into the ContentView of the new nib. I then had to fix up the constraints and rewire the connections to the custom subclass.

Chad Pavliska
  • 1,233
  • 12
  • 17
  • 6
    Or just add UITableViewCell from objects library in the same xib file, copypaste/relayout/relink IBOutlets/set base class, then remove old view. – jki Oct 02 '15 at 08:28
  • 1
    Thanks for this! Really frustrating. – Michael Doyle Oct 07 '15 at 03:17
  • 1
    Thank you so much for sharing. Wasted my whole day on this until I saw your answer. – user3144836 Apr 15 '17 at 03:27
  • Nice answer! I had this problema with legacy project. Worked like a charm. Thank you. – Gabriel Oliva Jan 27 '18 at 13:10
  • Great! Thanks a lot, waste half a day on it( – Anton Eregin Feb 17 '18 at 18:08
  • For me it was that the XIB I used as a collection view cell was of type, well, **View**. I've removed it and created a new class which extends `UICollectionViewCell`, and checked the **Also create XIB file** option. This created a XIB of type **CellView** instead of just **View**. The whole codebase remained the same, the only difference was that I was able to click the buttons in the cell now instead of always reaching CollectionView's `didSelectItemAt` when trying to. – Bogdan Razvan Oct 31 '18 at 14:56
17

Just add this line in tableView cellForRowAtIndexPath method

cell.contentView.userInteractionEnabled = false;
return cell;

but this is not a proper solution.

iOS 9, puts a content view to a tableViewCell, even though you use a custom cell/xib. The content view being added after the custom cell, it was on top of it.

To solve this properly just replace custom cell/xib's view with UITableviewCell from object library [from where you usually drag&drop UIButton, UILabel, etc...];

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Jayesh Lathiya
  • 788
  • 6
  • 18
  • This is an excellent solution if you want a quick fix. I was using a 3rd party framework that stopped working. All I had to do was disable userinterations on the contentview. I did this in initWithCoder (it was a nib file). – JRam13 Dec 11 '15 at 16:11