Modifying UITableViewCell, where to place this code? Confused about design patterns

trav.chung

I'm working my way through BigNerdRanch iOS Programming. I'm currently working on the Bronze challenge in Chapter 11 (Subclassing UITableViewCell)

Challenge:

Update the ItemCell to display the valueInDollars in green if the value is less than 50 and red if the value is greater than or equal to 50.

My solution is:

cell.valueLabel.textColor = item.valueInDollars < 50 ? UIColor.redColor() : UIColor.greenColor()

Now I placed this logic in my ItemsViewController (UITableViewController), tableView(cellForRowAtIndexPath) function.

// Get a new or recycled cell
    let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath) as! ItemCell

    // Update the labels for the new preferred text size
    cell.updateLabels()

    if (itemStore.allItems.count == indexPath.row) {
        cell.nameLabel.text = "No more items!"
        cell.serialNumberLabel.text = ""
        cell.valueLabel.text = ""
    } else {
        // Set the test on the cell with the description of the item
        // that is at the nth index of items, where n = row this cell
        // will appear in on the tableview
        let item = itemStore.allItems[indexPath.row]

        cell.nameLabel.text = item.name
        cell.serialNumberLabel.text = item.serialNumber
        cell.valueLabel.text = "$\(item.valueInDollars)"
        cell.valueLabel.textColor = item.valueInDollars < 50 ? UIColor.redColor() : UIColor.greenColor()
    }

    return cell

Is it better practice to place the logic in the controller or in the ItemCell class like such?

class ItemCell: UITableViewCell {

@IBOutlet var nameLabel: UILabel!
@IBOutlet var serialNumberLabel: UILabel!
@IBOutlet var valueLabel: UILabel!

func updateLabels() {
    let bodyFont = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
    nameLabel.font = bodyFont
    valueLabel.font = bodyFont

    let caption1Font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)
    serialNumberLabel.font = caption1Font
}

func updateValueTextColor(forValue value: Int) {
    valueLabel.textColor = value < 50 ? UIColor.redColor() : UIColor.greenColor()
}

}

In the previous chapter they talked about Dependency Inversion Principle and Design Patterns like MVC and Dependency Injection. Is this an application of one of these concepts? With Injection Dependency they mention that you don't want an object to assume which lower-level objects they need to use. Am I confusing this design pattern with Model View Controller where the Cell shouldn't know anything about the logic of the content? I'm trying to wrap my head around all these concepts and patterns and be able to identify em.

Sweeper

In my opinion, if ItemCell is specifically designed for displaying an Item, you should probably put the logic in ItemCell. If ItemCell is not specifically made to display Items, and it can also be used to display other stuff, then put the logic in the controller.

I don't know whether you noticed this, but some UIView subclasses have logic! UISegmentedControl needs to deselect the selected segment when the user selects another segment. UIButton "glows" a little bit when tapped on. UIPickerView makes a sound when scrolled. The views have logic because that is what they are designed for! UISegementedControl is designed to work like tabs, so the selected segment must be deselected when another segment is selected!

So if your ItemCell is specifically designed to display an Item, then you can put the logic in ItemCell.

However, I think that you shouldn't create a method for this. A property would be better:

var valueInDollars: Int {
    willSet {
        self.valueLabel.text = "$\(newValue)"
        self.valueLabel.textColor = newValue < 50 ? UIColor.redColor() : UIColor.greenColor()
    }
}

And then you can just do it like this:

cell.valueInDollars = item.valueInDollars

Note: you also need to initialize valueInDollars in the ItemCell's initializer, or you can just make it optional.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modifying UITableViewCell, where to place this code? Confused about design patterns

From Dev

Confused as to where to place logic code in ViewModel

From Dev

Confused about the following 'generics' code

From Dev

Confused about prestashop PDF generated process. Where is the code to handle the logic?

From Dev

Where to place blocks of code

From Dev

How to go about solving this (Design patterns)

From Dev

Confused about this php code using curly braces

From Dev

Confused about the big O notation of the code

From Dev

Confused about the ways to write code for instance variables

From Dev

Swift where to place code in viewcontrolles

From Dev

Where to place gui item code

From Dev

Material design: Where to place elevation in android styles?

From Dev

C++ code for copying FILES : : confused about relative address (tilde)

From Dev

Confused about this C code I saw in a job interview

From Dev

php: Confused about the way the "use" keyword is used in this code snippet

From Dev

I get confused about two lines source code in express

From Dev

Confused about this C code I saw in a job interview

From Dev

C++ code for copying FILES : : confused about relative address (tilde)

From Dev

confused about javascript code "require('./config/passport')(passport);"

From Dev

Best place for UI-related initialization code UITableViewCell with Storyboard

From Dev

Modifying Patterns with SED

From Dev

Where can I find a list of database design anti-patterns?

From Dev

django + uwsgi, where to place my startup code?

From Dev

Where to place StrictMode code within an Android app

From Dev

Where to place UIView animation code in VIPER?

From Dev

Where place forever-monitor code?

From Dev

Unit Tests - where to place initialization code?

From Dev

Configure Automatic Updates in WordPress - Where to place code

From Dev

Not sure where to place fragment activity code

Related Related

  1. 1

    Modifying UITableViewCell, where to place this code? Confused about design patterns

  2. 2

    Confused as to where to place logic code in ViewModel

  3. 3

    Confused about the following 'generics' code

  4. 4

    Confused about prestashop PDF generated process. Where is the code to handle the logic?

  5. 5

    Where to place blocks of code

  6. 6

    How to go about solving this (Design patterns)

  7. 7

    Confused about this php code using curly braces

  8. 8

    Confused about the big O notation of the code

  9. 9

    Confused about the ways to write code for instance variables

  10. 10

    Swift where to place code in viewcontrolles

  11. 11

    Where to place gui item code

  12. 12

    Material design: Where to place elevation in android styles?

  13. 13

    C++ code for copying FILES : : confused about relative address (tilde)

  14. 14

    Confused about this C code I saw in a job interview

  15. 15

    php: Confused about the way the "use" keyword is used in this code snippet

  16. 16

    I get confused about two lines source code in express

  17. 17

    Confused about this C code I saw in a job interview

  18. 18

    C++ code for copying FILES : : confused about relative address (tilde)

  19. 19

    confused about javascript code "require('./config/passport')(passport);"

  20. 20

    Best place for UI-related initialization code UITableViewCell with Storyboard

  21. 21

    Modifying Patterns with SED

  22. 22

    Where can I find a list of database design anti-patterns?

  23. 23

    django + uwsgi, where to place my startup code?

  24. 24

    Where to place StrictMode code within an Android app

  25. 25

    Where to place UIView animation code in VIPER?

  26. 26

    Where place forever-monitor code?

  27. 27

    Unit Tests - where to place initialization code?

  28. 28

    Configure Automatic Updates in WordPress - Where to place code

  29. 29

    Not sure where to place fragment activity code

HotTag

Archive