将对象/结构传递给 UITableViewCell,或在 configureCell:atIndexPath:? 中配置单元格
我需要使用模型对象的内容配置自定义表视图单元格。哪种设计模式是首选?我不是在寻找“正确”的答案,只是对利弊进行很好的讨论。
1)在configureCell:atIndexPath:?中手动配置cell
- configureCell:atIndexPath:
{
myCell.field1 = modelObject.field1 ;
myCell.field2 = modelObject.field2 ;
}
2)只需编写自定义单元格以接受模型对象并将模型对象批量传递到configureCell:atIndexPath:?
- configureCell:atIndexPath:
{
myCell.model = modelObject ;
}
根据德米特定律,也许(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)将单元格作为ViewModel的行为:
- configureCell:atIndexPath:
{
myCell.model = modelObject ; // cell has direct access to model behavior
}
// Call behavior in cell
[self.model myBehavior] ;
I need to configure a custom table view cell with the contents of a model object. Which is the preferred design pattern? I'm not looking for the "right" answer, just a good discussion of the pros and cons.
1) Manually configure the cell in configureCell:atIndexPath:?
- configureCell:atIndexPath:
{
myCell.field1 = modelObject.field1 ;
myCell.field2 = modelObject.field2 ;
}
2) Just code the custom cell to accept the model object and pass the model object wholesale to the cell in configureCell:atIndexPath:?
- configureCell:atIndexPath:
{
myCell.model = modelObject ;
}
According to the Law of Demeter, perhaps (1) is better. However, when the cell needs to handle user actions, is a tableview a more appropriate controller, or the cell itself (use the cell as a ViewModel)? I think definitely the latter may make for clearer code:
1) Behaviors with table view as controller
// 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) Behavior with the cell as a ViewModel:
- configureCell:atIndexPath:
{
myCell.model = modelObject ; // cell has direct access to model behavior
}
// Call behavior in cell
[self.model myBehavior] ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我更喜欢创建自定义单元子类并向其传递整个模型对象以对其自身进行配置的方法。
如果您后来决定单元格需要使用模型中的额外字段,并且让您可以自由更改单元格的视觉布局和模型的内部结构,而无需更新视图控制器,那么它是最大的灵活性。
I prefer the approach of creating a custom cell subclass and passing it the whole model object to configure itself with.
It's maximally flexible if you later decide that the cell needs to use extra fields from the model and leaves you free to change the cell's visual layout and the model's internal structure without having to also update your view controller.
我是#2 某种形式的粉丝。
我认为控制器不必设置单元中的所有内容,但我也不认为单元保留模型的时间不应超过其本身的时间。因此,我的单元子类具有以下形式的方法:
该函数不保存对模型的引用,但使用模型的属性来更新单元的状态。我更喜欢让我的所有模型都支持 CoreData,因此 NSFetchedResultsController 提供了再次正确更新单元格的钩子。
I am a fan of a certain form of #2.
I don't think the controller should have to setup everything in the cell, but I also don't think the cell should hold onto a model longer than it has too. So my cell subclasses have a method in the form of:
That function does not hold a reference to the model, but uses the properties of the model to update the cell's state. I prefer to make all my models CoreData backed, and so NSFetchedResultsController's provides the hooks to properly update the cell again.
很晚才得到答案,但这仍然是我的想法。我会选择选项1。为什么是因为 UITableViewCell 是一个视图,为了可重用性,我不会向它传递一个对象。我不会将模型与视图结合起来。我宁愿在 cellForRowAtIndexPath 中使用它。这是我的想法
Very late to the answer, still here is my thought. I would go with option1. Why because the UITableViewCell is a View and for the sake of reusability I would not pass it an object. I would not couple model with View. I would rather prefer to use it in cellForRowAtIndexPath. This is my thought