I'd like to if there's a cleaner way to customize prototype cells than what I am currently doing as I feel as though I'm not writing clean and efficient code. Currently I have VC1 that segues to VC2, and both share the same Custom Cell Class to customize my tableview cells.
CustomCell:
class CustomCell: UITableViewCell
{
// VC1
@IBOutlet var someImageView: UIImageView!
@IBOutlet var someLabel: UILabel!
// VC2
@IBOutlet var otherImageView: UIImageView!
@IBOutlet var otherLabel: UILabel!
....
customizeCellsByScreenSize()
{
if screen size is iPhone 5
if someImageView != nil && someLabel != nil
{
// We are in VC1, customize cells
}
else if otherImageView != nil && otherLabel != nil
{
// We are in VC2, customize cells
}
if screen size is iPhone 6
// Do the same check as above
if screen size is iPhone 6 Plus
// DO same check as above
}
}
VC1:
class VC1: UIViewController
{
@IBOutlet var someImageView: UIImageView!
@IBOutlet var someLabel: UILabel!
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
...
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
...
}
}
VC2:
class VC2: UIViewController
{
@IBOutlet var otherImageView: UIImageView!
@IBOutlet var otherLabel: UILabel!
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
...
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
...
}
}
In the CustomCell
class, I check what iPhone screen size is first, then customize the cells based on that, but to ensure I don't get a nil if I'm not in VC1 vs VC2, I have to check whether the IBOutlets
are nil first to determine what ViewController
I'm in before customize the cells.
This gets rather repetitive and messy for each check, and I like to implement a better method.
I know I can use a protocol and have VC1 and VC2 conform to it, then add a standard blueprint naming convention of the IBOutlets
, but is a protocol ideal for this purpose?
Or is there a better alternative to implement that I can make my code cleaner?