简体   繁体   中英

Customize TableView Cells from ViewControllers sharing the same Custom Cell Class?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM