How to subclass an UIViewController with UITableViewDelegate in many subclasses
What you're trying to do doesn't really work that way. The @IBOutlets are all connected through the Storyboard - but they don't "flow through" in the pattern you're hoping to use.
You can do it - in a way. Take a look a the discussion and answers in this SO post: How to subclass custom UIViewController in Swift?
Alternatively, if you're goal is to use a "common" table view controller - but allow customization - you can create separate UITableViewDelegate
and UITableViewDataSource
files, plus separate visually designed cells, and then assign them via code as desired.
Subclassing UITableViewController in Swift
Maddening, isn't it? But the error message tells you what to do: if you are going to call super.init(style:)
, then you must also implement init(nibName:bundle:)
, even if all you do there is to call super
:
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName:nibNameOrNil, bundle:nibBundleOrNil)
}
This, however, raises the question of what to do about properties. Swift won't let you implement init(nibName:bundle:)
unless it initializes all uninitialized properties.
One obvious solution is to make your properties Optionals; now the implementation shown above is legal. In
init(scheduleController:)
, you will have to initialize your properties after callingsuper.init
.Less dramatically, make your property a
var
but not an Optional. Now initialize it multiple times: beforesuper.init
in all your initializers, and also aftersuper.init
in yourinit(scheduleController:)
.
None of those, however, are the solution I use in my own code. In my code, I do not call super.init(style:)
, as you are doing — I call super.init(nibName:bundle:)
instead. This works because:
The default style is
.Plain
, which is what you wantEven if what you want is
.Grouped
, you can specify that in the associated nib file
The big advantage of this approach is that it permits your properties to be a non-Optional let
, which is probably what you really would like to do.
Subclass UITableView in Swift
You're probably looking to override numberOfSections
instead of numberOfSectionsInTableView:
, which is a method on the UITableViewDataSource
protocol. However, I believe it's more wise to create your own subclass of UITableViewController
or your class conforming to UITableViewDataSource
and set it as your table view's delegate rather than of the table view itself. Subclassing UIView
and its descendants is usually reserved for customizing view-specific functionality and attributes, such as adding custom drawing or custom touch handling.
How to properly override init() for UITableViewController subclass?
Problem is that if inside guard
value isn't assigned, super.init(...)
isn't called and non-optional variables aren’t assigned as well. But you want to throw fatalError
in else {...}
so calling super.init(...)
here wouldn’t make any sense.
So first call super.init(...)
and then do other stuff
override init(...) {
super.init(...)
... // do other stuff
}
then make sure that you assign all non-optional global variables before you call super.init(...)
. If don't (like your case), make these variables optional
var variable: Type?
How can I subclass a UITableView?
I think, you should still go with a Controller class. I expect subclassing UITableView to be tedious work — if possible with reasonable amount at all.
There is no problem to have UIViewController/NoViewController implemented the delegate and datasource and yet assign another controller to a specific tableView. note, that the datasource and delegate don't need to be subclasses of UITableViewController.
have a look at this answer: Implement Delegate at Run Time?
My UITableView does not delegate to its view controller, so that's not the problem.
You have to have to use delegate and datasource, that is how TableViews are filled and configured. otherwise you will have to overwrite every method of UITableView — including private ones, a no-go if you want into AppStore. Recreating UITableView without subclassing it would be even easier.
Related Topics
Left-Align Image and Center Text on Uibutton
How to Apply Filter to Video Real-Time Using Swift
Sendasynchronousrequest Was Deprecated in iOS 9, How to Alter Code to Fix
Swiftui: Unwanted Split View on iPad
How to Select Range of Values When Using Arc4Random()
iOS 11 Core Nfc - Any Sample Code
Number of Days in the Current Month Using iOS
Removing Badge from iOS App Icon
Xcode6 Error: "No Matching Provisioning Profiles Found for Application"
How to Get the Build Uuid in Runtime and the Image Base Address
Uicollectionview Horizontal Paging Not Centered
How to Stop Firebase from Logging Status Updates When App Is Launched
How to Implement "Share Button" in Swift
Ios: Determine If Device Language Is Right to Left (Rtl)
Uiview. Why Does a Subviews Outside Its Parent's Extent Not Receive Touches
Xcframework with Pods Dependencies