iOS Swift - UITableViewCell Custom Subclass not displaying content
I believe it has to do with the sizing in Storyboard. It seems now it likes to default to a wider view and most people including me (and judging by your storyboard view width, you) prefer having the width set to 'Compact'.
In storyboard either try setting your width/height to any/any or in the inspector for your labels inside the cells scroll all the way down and play the 'Installed' checkbox and you'll notice it has various options for the sizing class. If it's like mine, you'll have the first 'Installed' one unchecked and a second one for your sizing option checked. I removed the custom 'Installed' and checked the default one and then moved my labels into place.
I don't believe I have enough reputation to post more than 1 image or 2 links, wish I could to explain what I mean easier.
Why is my custom UITableViewCell showing not displaying properly?
You have the table view controller and prototype cell in the storyboard. But you are creating the view controller programmatically
let v = ControlSettingsView()
So the prototype cell properties will be nil. Create the view controller instance using storyboard identifier
Make sure you've set storyboard identifier ControlSettingsView
for the view controller
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ControlSettingsView") as? ControlSettingsView {
self.navigationController?.pushViewController(vc, animated: true)
}
And don't use register(_:forCellReuseIdentifier:) method if you have the tableview cell in the storyboard.
How to subclass a UITableViewCell?
That is the right way to subclass. But if you are using Storyboards or Xib files then you need to reference the CreateChannelCell
in the UITableView
that you created and where the CreateChannelCell
is located.
You need to select the cell and then go to CustomClass
and write the name of your class there ... in this case CreateChannelCell
If you are not using Storyboards or Xib's please see the answer provided by Code Different
How to subclass custom UITableViewCell?
Your variable questionLabel
is already defined in your superclass. No need to mention it again. That's the whole point of inheritance. Your subclasses inherit the variables of its super classes.
IBOutlet, IBAction, IBDesignable are just labels. Doesn't matter where you add them in the inheritance tree. Once there, you have informed the compiler/Xcode that this is a "special" function or variable.
So if your class has a function @IBAction func doStuff()
, you can override it as override func doStuff()
and still connect to it from within IB. Same for overriding IBOutlet in case you want to add willSet
or didSet
or replace the getter/setter functions.
Custom UITableviewCells displayed correctly but only one in the section is the correct class
The problem arises when I hit the "done" button, my program traverses
through all the cells, gathering the data from each cell and updating
the model
This is your problem. Your cells should reflect your data model, they cannot be relied upon to hold data as once the cell is offscreen it may be re-used for a row that is onscreen.
If data is updated by the user interacting with the cells then you should update your data model as they do that. You can use a temporary store if you don't want to commit changes immediately.
Using a programmatically created UITableViewCell subclass in Swift
Ok first a few important comments.
First, you are needlessly creating your own new cell every time the table view requests a cell instead of reusing old cells. You should remove this line:
cell = EventCell(style: .Default, reuseIdentifier: cellIdendifier)
That is unnecessary because dequeue will automatically create new cells as needed based on the class you have registered for the given identifier.
Second, you should not be using the main screen bounds when laying out your code. This will break down if your table view is not the full width. Instead, you can use self.bounds
so it is always relative to the cell itself.
Third, you should not be calling setNeedsLayout or layoutIfNeeded because if that method is called, it is already laying out everything again.
Fourth, you should register your table view cell class before setting the table view data source just in case UITableView every starts requesting things from data source when the data source is set.
Fifth, two of your subviews have a size of 0,0 so they are not going to show up anyway.
If this code is indeed running without crashing then you are creating EventCells because you are doing a forced casting from the result of the dequeueReusableCellWithIdentifier:forIndexPath
. That means you simply have a layout / display / data issue.
Related Topics
How Have Multiple Init() with Swift
Implementing Stringliteralconvertible on Nsurl
Swift Callkit Sometimes Can't Activate Loudspeaker After Received Call (Only Incoming Call)
Data Ranged Subscribe Strange Behavior
How to Convert a Unsafemutablepointer<Void> to Uint8
Why Is Casting a Struct to Anyobject Not a Compile Error in Swift
Why Do We Need to Set Delegate to Self? Why Isn't It Defaulted by the Compiler
Response Struct Does Not Like Codingkeys
Swift Firestore Search for Users
Skscene Becomes Unresponsive While Being Idle
Use Quick Look Inside a Swift Cocoa Application to Preview Audio Files
Enum's Rawvalue Property Not Recognized
Swift iOS 9: Section Header Change Position After Reload Data
How to Make Uicollectionview Reload Once It Receives Data from Firebase
Firebase Access Keys in Queryorderby
Replace Exactly One Pixel in an Image and Put It in Another Image via Swift