I created test project to reproduce the issue https://github.com/msnazarow/DiffarableTest
Simply you need to pass several steps
- Create new target
- Create
Baseclass that inheritUITableViewDiffableDataSourceand alsoUITableViewDelegatebut do not implementUITableViewDelegatemethods - In another target(main for simplify) inherit
Baseclass and implement any ofUITableViewDelegatemethod (for exampledidSelectRowAt) - None of implementing methods would work
You have to configure the delegate funcs in your
BaseDataSourceclass to be overridden in subclasses.So, first step, comment-out your
didSelectRowAtfunc inextension MyDataSource:and implement
didSelectRowAtinBaseDataSource:When you run the app and tap on the 3rd row, you should get debug output:
To implement that in your subclass, you can override it:
You'll get an error: Cannot override a non-dynamic class declaration from an extension ... so make it dynamic in
BaseDataSource:Tapping on the 3rd row now should output:
Note that
didSelectRowAtinBaseDataSourcewill be called if there is no sub-classed delegate. Plus, if you want some code to also run indidSelectRowAtinBaseDataSource, you can callsuperfrom the subclass:and selecting the 3rd row outputs:
Edit
After a little more research, this is considered by some to be a "bug" as it seems to have changed between Swift versions.
However, one big change along the way is that Swift originally did, but no longer, infers
@objc. Everyone ran into that when it became necessary to add@objcto funcs such as when used in selectors.So, two ways to handle this...
First, as shown above, implement "do nothing" funcs for any table view delegate funcs you want to override in your subclass.
The second option, which sounds like you would prefer, is to declare the
@objcmethod inside your subclass (or its extension):Now you can go back to your original code, and you only need to add that one line to get the functionality needed.