モデルオブジェクトの内容でカスタムテーブルビューセルを構成する必要があります。どちらが好ましいデザインパターンですか?私は「正しい」答えを探しているわけではなく、賛否両論の良い議論を探しています。オブジェクト/構造体をUITableViewCellに渡すか、configureCellでセルを設定します:atIndexPath :?
1)configureCellでセルを手動で設定する:atIndexPath :?
- configureCell:atIndexPath:
{
myCell.field1 = modelObject.field1 ;
myCell.field2 = modelObject.field2 ;
}
2)だけでモデルオブジェクトを受け入れ、configureCellのセルにモデルオブジェクトの卸売を渡すためにカスタムセルをコーディング:atIndexPathを:?
- configureCell:atIndexPath:
{
myCell.model = modelObject ;
}
Law of Demeterによれば、おそらく(1)が優れています。しかし、セルがユーザアクションを処理する必要がある場合、テーブルビューの方が適切なコントローラか、セル自体(セルをViewModelとして使用しますか)ですか?私は後者は明確コードのために作ることができる確かに考える:コントローラとして表ビュー
1)ビヘイビア
// tableview
- configureCell:atIndexPath:
{
myCell.field1 = model.field1 ;
myCell.button.delegate = myDelegate ; // have to define MyDelegate protocol
}
// Call behavior in cell
[self.button.delegate delegateCall] ;
// delegate
- (void) delegateCall
{
// find out which cell was manipulated
// find the object associated with the table cell
[theFoundModelObject myBehavior] ;
}
ビューモデルとして細胞と2)行動:私は好む
- configureCell:atIndexPath:
{
myCell.model = modelObject ; // cell has direct access to model behavior
}
// Call behavior in cell
[self.model myBehavior] ;