Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 修改UITableViewCell,将此代码放置在何处?对设计模式感到困惑_Ios_Swift_Design Patterns_Model View Controller_Dependency Injection - Fatal编程技术网

Ios 修改UITableViewCell,将此代码放置在何处?对设计模式感到困惑

Ios 修改UITableViewCell,将此代码放置在何处?对设计模式感到困惑,ios,swift,design-patterns,model-view-controller,dependency-injection,Ios,Swift,Design Patterns,Model View Controller,Dependency Injection,我正在通过BigNerdRanch的iOS编程工作。我目前正在进行第11章(UITableViewCell子类化)中的铜牌挑战 挑战: 如果值小于50,则更新ItemCell以绿色显示值视差;如果值大于或等于50,则更新ItemCell以红色显示值视差 我的解决办法是: cell.valueLabel.textColor = item.valueInDollars < 50 ? UIColor.redColor() : UIColor.greenColor() cell.valueLab

我正在通过BigNerdRanch的iOS编程工作。我目前正在进行第11章(UITableViewCell子类化)中的铜牌挑战

挑战:

如果值小于50,则更新ItemCell以绿色显示值视差;如果值大于或等于50,则更新ItemCell以红色显示值视差

我的解决办法是:

cell.valueLabel.textColor = item.valueInDollars < 50 ? UIColor.redColor() : UIColor.greenColor()
cell.valueLabel.textColor=item.valueInDollars<50?UIColor.redColor():UIColor.greenColor()
现在,我将此逻辑放在ItemsViewController(UITableViewController)、tableView(cellforrowatinexpath)函数中

// 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
//获取新的或回收的单元
让cell=tableView.dequeueReusableCellWithIdentifier(“ItemCell”,forIndexPath:indexPath)为!项目单元
//为新的首选文本大小更新标签
cell.updateLabels()
if(itemStore.allItems.count==indexPath.row){
cell.namelab.text=“没有其他项目!”
cell.serialNumberLabel.text=“”
cell.valueLabel.text=“”
}否则{
//在带有项目描述的单元格上设置测试
//即在项目的第n个索引处,其中n=此单元格的行
//将显示在tableview中
让item=itemStore.allItems[indexPath.row]
cell.namelab.text=item.name
cell.serialNumberLabel.text=item.serialNumber
cell.valueLabel.text=“$\(item.valueInDollars)”
cell.valueLabel.textColor=item.valueInDollars<50?UIColor.redColor():UIColor.greenColor()
}
返回单元
将逻辑放在控制器或类似的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()
}
class ItemCell:UITableViewCell{
@IBOutlet var名称标签:UILabel!
@IBVAR serialNumberLabel:UILabel!
@IBOutlet var valueLabel:UILabel!
func updateLabels(){
让bodyFont=UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
namelab.font=bodyFont
valueLabel.font=bodyFont
让caption1Font=UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)
serialNumberLabel.font=caption1Font
}
func updateValueTextColor(forValue:Int){
valueLabel.textColor=值<50?UIColor.redColor():UIColor.greenColor()
}
}


在上一章中,他们讨论了依赖项反转原理和设计模式,如MVC和依赖项注入。这是这些概念之一的应用吗?对于注入依赖性,他们提到您不希望对象假定需要使用哪个较低级别的对象。我是否将这种设计模式与模型-视图-控制器混淆了?在模型-视图-控制器中,单元不应该知道任何内容的逻辑?我正试图将我的头脑集中在所有这些概念和模式上,并能够识别它们。

在我看来,如果
ItemCell
是专门为显示
项目而设计的,那么您可能应该将逻辑放在
ItemCell
中。如果
ItemCell
不是专门用来显示
Item
s的,它也可以用来显示其他内容,那么将逻辑放入控制器中

我不知道您是否注意到了这一点,但是一些
UIView
子类具有逻辑性
UISegmentedControl
需要在用户选择另一段时取消选择所选段<代码>ui按钮
在被点击时会有一点“发光”
UIPickerView
在滚动时发出声音。视图具有逻辑性,因为这就是它们的设计目的
UISegementedControl
设计为与选项卡一样工作,因此在选择其他段时必须取消选择所选段

因此,如果您的
ItemCell
是专门设计用来显示
项目的,那么您可以将逻辑放在
ItemCell

但是,我认为您不应该为此创建方法。一处房产会更好:

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

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

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

var valueInDollars: Int = 0 {
   willSet {
      self.valueLabel.text      = "$\(newValue)"
      self.valueLabel.textColor = newValue < 50 ? UIColor.red : UIColor.green
   }
}
var值indollars:Int=0{
意志{
self.valueLabel.text=“$\(newValue)”
self.valueLabel.textColor=newValue<50?UIColor.red:UIColor.green
}
}
添加到ItemCell.swift中并替换

cell.valueLabel.text=“$\(item.valueInDollars)”

cell.valueInDollars=item.valueInDollars


<> >强> > IsvsVieldCurror。Swift >P/>如果你认为我的答案对你有帮助,请考虑点击这个复选标记来接受答案。@用户2521203
var valueInDollars: Int = 0 {
   willSet {
      self.valueLabel.text      = "$\(newValue)"
      self.valueLabel.textColor = newValue < 50 ? UIColor.red : UIColor.green
   }
}