思い立ったが吉日!

iOSが好きです。

共通系CustomTableViewCell

## 共通系CustomTableViewCell

tableViewでCellに値をセットするとき、

cell.typeLabel.text = model.title
cell.moneyLabel.text = model.money
if let money = model.money where money > 0 {
     cell.moneyLabel.textColor = UIColor.BlueColor()
} else {
     cell.moneyLabel.textColor = UIColor.RedColor()
}
cell.term.text = model.term
cell.memo.text = model.memo

こんな感じで書いてたんですが、

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}が膨れ上がってしまうので、cellの個々の値はちゃんとcell側で書いてあげた方がいいですね。

TableView側でモデルを渡してあげる

cell.model = model

CustomCell側

// 渡されたmodelを元に個々の要素に値をセットしていく
var model: CellModel? {
        didSet {
            cellTitleLabel.text = {
                if let title = model?.title {
                    return title
                }
                return ""
            }()
            moneyLabel.text = {
                if let money = model?.money {
                    return money
                }
                return "0"
            }()
            if let money = model?.money where money > 0 {
                  cell.moneyLabel.textColor = UIColor.BlueColor()
            } else {
                  cell.moneyLabel.textColor = UIColor.RedColor()
            }

            term.text = model?.term
            memo.text = model?.memo
        }
    }