【问题标题】:Modifying UITableViewCell, where to place this code? Confused about design patterns修改UITableViewCell,这段代码放在哪里?对设计模式感到困惑
【发布时间】:2016-07-17 23:53:55
【问题描述】:

我正在学习 BigNerdRanch iOS 编程。我目前正在处理第 11 章(继承 UITableViewCell)中的铜牌挑战

挑战:

更新 ItemCell 以在值小于 50 时以绿色显示 valueInDollars,如果值大于或等于 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

将逻辑放置在控制器或 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 和依赖注入。这是这些概念之一的应用吗?通过注入依赖,他们提到您不希望对象假定他们需要使用哪些较低级别的对象。我是否将这种设计模式与模型视图控制器混淆了,其中单元不应该对内容的逻辑一无所知?我正试图围绕所有这些概念和模式进行思考,并能够识别它们。

【问题讨论】:

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


    【解决方案1】:

    在我看来,如果ItemCell 是专门为显示Item 而设计的,那么您可能应该将逻辑放在ItemCell 中。如果ItemCell不是专门用来显示Items的,也可以用来显示其他的东西,那就把逻辑放到控制器里面。

    我不知道你是否注意到这一点,但一些UIView 子类有逻辑! UISegmentedControl 需要在用户选择另一个段时取消选择选定的段。 UIButton 点击时会“发光”一点。 UIPickerView 滚动时会发出声音。这些视图具有逻辑性,因为这就是它们的设计目的! 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,或者您可以将其设为可选。

    【讨论】:

    • 如果您认为我的回答对您有所帮助,请考虑通过单击该复选标记来接受该回答。 @user2521203
    【解决方案2】:

    为了简化使用 Swift 3 的下一个用户,解决方案应该是:

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

    添加到 ItemCell.swift 并替换

    cell.valueLabel.text = "$\(item.valueInDollars)"

    cell.valueInDollars = item.valueInDollars

    ItemsVieController.swift中。

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多