Reputation: 2485
I have a custom table view cell which I create pragmatically. No IBOutlets are used.
I want to use it in interface Builder and in code.
I want to use it in interface builder for static table view controllers.
In code for dynamic table view controllers
I will not use .xib files.
After reading for days this is what I have learned so far.
// This method will be called when I create the cell programmatically
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
// This will be called when a file is loaded from storyboard (or .xib)
// will not use xib only storyboard file (static table view cell)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
// this will be called after init(coder)
// when all IBOutlets are ready for use
// I'm currently not using this method
// because I don't have any IBOutlets
// Not sure if I should use it
override func awakeFromNib() {
super.awakeFromNib()
}
Everything seems to work fine.
When using the cell in code I'm manually registering the cell and reusing it.
Is this code good ? I can't seem to find any clear explanation of how exactly everything works. I read everything that I could find online but I keep finding diffrent opinions and ideas. I want a clear explanation of when each initializer method should be used.
For example some code online uses the awakeFromNib and init(coder) methods. They seem to call commonInit()
in both of those methods.
I really don't see any reason for that because those are called one after the other.
Upvotes: 0
Views: 325
Reputation: 5303
Creating and using a UITableViewCell entirely in code is not hard to do. I'll give an example of the simplest cell you can create. You can read the docs to see other, more sophisticated cell configurations (with images, subtitles, etc)
at the top of your class (after the opening brace), you should define an identifier for your new cell with a different identifier than the one you used in your storyboard.
let cellIdentifier = "Cell"
in viewDidLoad you need to register the new cell :
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
then, when you need a cell, you ask for it using the identifier you defined earlier:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
cell?.textLabel?.text = dataSource[indexPath.row].title
return cell!
}
Finally:
The only changes you need to make when using cells from a nib/xib rather than purely from code are:
let cellNib = UINib(nibName: "DropDownCell", bundle: Bundle(for: DropDownCell.self))
load the cell from disk rather than simply an initializer, and when you register the cell, you register the nib you just created:
tableView.register(cellNib, forCellReuseIdentifier: cellReuseIdentifier)
The Apple Swift documentation is always the best place to start, to see how things are meant to be used. Swift 3, "Swift and Cocoa"
Matt Neuberg (he is very active on this site) has written a number of books that I've bought, and his "Programming iOS" series have a chapter on configuring layout with cells in nibs.
There's also an online course in Swift, HackingWithSwift, which is free, and has a chapter on UITableViewCells. It shows you how to colorize the text in your cell to make it pop a bit. UITableViewCells
Many people swear by the tutorials found here: Ray Wenderlich The nice thing about these is you can download before and after snapshots of the code, so you see everything working.
Here's one on self-sizing cells using autolayout Wenderlich - Autolayout Cells
And you can come here when you get really stuck [you've googled and still can't figure it out]. Try to ask very specific questions with an example of what you need, and show what you have tried. I think you'll be surprised how quickly someone will help you when it's clear what is missing from the picture.
I think I've gone a bit overboard here, because it's clear you know what you're doing... Keep it up, and 🍀!
Upvotes: 0