0

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Pangu
  • 3,721
  • 11
  • 53
  • 120
  • 1
    Why dont you use autolayout instead of checking? and why need to make different outlets for different vc? whats your design look like? – Tj3n Dec 07 '16 at 05:05
  • @pangu Use 1 imageView and 1 label. Just change the image & text based on the view controllers. – Venk Dec 07 '16 at 05:17
  • If your two view controllers are largely similar you can add a BOOL parameter to check which controller you want to display to set the difference and all there will be no need to repeat codes, there might be no need for two different classes. – Ben Ong Dec 07 '16 at 05:25
  • @BenOng: how is that different then what I'm currently doing?..I still need to check what screen size is detected, so wouldn't I need to add a BOOL in every check? – Pangu Dec 07 '16 at 05:44
  • @Tj3n I am using autolayout...the different outlets are linked in the different VC...autolayout doesn't take into account being able to customize prototype cells beyond the standard format does it?..otherwise how am I suppose to change the text size/image size if screen is small/big – Pangu Dec 07 '16 at 05:47
  • My suggestion does not reduce the amount of checks required, it helps to make the codes cleaner so you do not need to maintain two sets of similar codes. – Ben Ong Dec 07 '16 at 05:47
  • @Venkat I am using autolayout in both VCs, so I cant just use 1 imageView/1 Label without having to programmatically deal with constraints – Pangu Dec 07 '16 at 05:48
  • If you want to reduce checks for screen size you can try using constraints that adjust your tableview to a percentage of the screen size, subsequently constraint your content of cell to a percentage of the cell size – Ben Ong Dec 07 '16 at 05:51

2 Answers2

0
enum TypeOfViewController {
    case vc1
    case vc2
}
class CustomCell: UITableViewCell
{

@IBOutlet weak var someImageView: UIImageView!
@IBOutlet weak var someLabel: UILabel!
var vcType: TypeOfViewController!


    func customizeCellsByViewController()
    { 
         switch vcType {
              case vc1:
              // do things that concern VC1
              case vc2:
              // do things that concern VC2
         }


    }
 func customizeCellsBySize() {
       if screen size is iPhone 5
        //DO things that concern size

       if screen size is iPhone 6
        // DO things that concern size

       if screen size is iPhone 6 Plus
        // DO things that concern size
    }
}

though i strongly recommend to use autolayout for setting height of table view cell. you can find a good tutorial here

Mohammadalijf
  • 1,387
  • 9
  • 19
  • Please read my problem description carefully, I'm not looking to change height of cell. All cells will be same height, but need to change contents within it based on screen size. – Pangu Dec 08 '16 at 03:52
0

There is an UITableViewCell resizing feature that is supported by UITableView itself.And you are making difficult things to your self even Apple has made easy for you to do.

https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-nsattributedstring

Else, use different custom cell for each VC

Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
  • Maybe my description was not clear enough so if not, I apologize, but I'm not looking to dynamically resize cells based on the contents within it, I'm looking to change the UI of the contents within the cells (where all cells are the same size), but the UI will change (i.e. font size) based on screen size. Therefore, I may end up implementing your 2nd suggestion of creating a different custom cell class for each VC. – Pangu Dec 08 '16 at 03:51
  • Remember you can use one or more different cells in each uitableview. And it will automatically resize base on it's inner contents on above tutorial – Thiha Aung Dec 08 '16 at 03:56
  • About the fontsize, yes you can configure it base on the device you have at UILabel Attributes Inspector. – Thiha Aung Dec 08 '16 at 03:59
  • Something similar, http://stackoverflow.com/questions/30774671/uitableview-with-more-than-one-custom-cells-with-swift – Thiha Aung Dec 08 '16 at 04:01