ios - 修改UITableViewCell,这段代码放在哪里?对设计模式感到困惑

标签 ios swift design-patterns model-view-controller dependency-injection

我正在努力完成 BigNerdRanch iOS 编程。我目前正在处理第 11 章(UITableViewCell 子类化)中的铜牌挑战赛

挑战:

更新 ItemCell,如果 valueInDollars 的值小于 50,则显示为绿色;如果值大于或等于 50,则显示为红色。

我的解决方案是:

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

现在我将此逻辑放入我的 ItemsViewController (UITableViewController), tableView(cellForRowAtIndexPath) 函数中。

// 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

将逻辑放置在 Controller 中或像这样的 ItemCell 类中是否是更好的做法?

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()
}

}

在上一章中,他们讨论了依赖倒置原则和设计模式,例如 MVC 和依赖注入(inject)。这是这些概念之一的应用吗?对于注入(inject)依赖,他们提到您不希望对象假设他们需要使用哪些较低级别的对象。我是否将此设计模式与 Model View Controller 混淆了,其中单元不应该了解内容的逻辑?我正在尝试理解所有这些概念和模式,并能够识别它们。

最佳答案

在我看来,如果 ItemCell 是专门为显示 Item 而设计的,那么您可能应该将逻辑放在 ItemCell 中。如果 ItemCell 不是专门用来显示 Item 的,并且它也可以用于显示其他内容,那么请将逻辑放入 Controller 中。

不知道你是否注意到这一点,但是一些UIView子类是有逻辑的!当用户选择另一个片段时,UISegmentedControl需要取消选择所选的片段。 UIButton 点击时会稍微“发光”。 UIPickerView 滚动时发出声音。这些 View 具有逻辑性,因为这就是它们的设计目的! UISegementedControl 被设计为像选项卡一样工作,因此当选择另一个段时,必须取消选择所选段!

因此,如果您的 ItemCell 专门设计用于显示 Item,那么您可以将逻辑放入 ItemCell 中。

但是,我认为您不应该为此创建方法。有属性会更好:

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

然后你就可以这样做:

cell.valueInDollars = item.valueInDollars

注意:您还需要在 ItemCell 的初始化程序中初始化 valueInDollars,或者您也可以将其设置为可选。

关于ios - 修改UITableViewCell,这段代码放在哪里?对设计模式感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38427025/

相关文章:

iOS - 可编辑 subview (TextView、标签等)返回 null

swift - NSFetchRequest ReturnsDistinctResults 给出空结果

javascript - Swift - 从 WebView 获取 js 对象

ios - SpriteKit - 对在多个 SKNode 上运行的多个 SKAction 进行排序

c# - 如何正确处理元素

design-patterns - 什么时候应该使用单例模式而不是静态类?

ios - 警告 : itunes store operation failed error (null)

ios - uiview的圆角看起来不太好,uiview

objective-c - 在 iOS CFStreams 上设置代理不起作用

language-agnostic - 为什么大多数系统架构师都坚持首先对接口(interface)进行编程?