dimanche 17 juillet 2016

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

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.

Aucun commentaire:

Enregistrer un commentaire