简体   繁体   English

我应该将数据存储在自定义UITableView中吗?

[英]Should I store data in my custom UITableView?

I have 2 viewControllers that each contains one UITableView among other subviews. 我有2个viewControllers,每个viewControllers包含一个UITableView和其他子视图。 The 2 UITableViews have the same section structure and cell types, but the data source is different. 2个UITableView具有相同的节结构和单元格类型,但数据源不同。

So I decided to extract this UITableView into a custom UITableView. 因此,我决定将此UITableView提取到自定义UITableView中。 The custom UITableView implements methods like tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , and is its own data source and delegate. 自定义UITableView实现了诸如tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ,并且是其自己的数据源和委托。 The viewController only provides data. viewController仅提供数据。

The question is I can't figure out which is the best way for viewControllers to provide data. 问题是我不知道哪种方法是viewController提供数据的最佳方法。 I can think of 2 ways: 我可以想到两种方式:

first way 第一路

in MyCustomTableView.h: 在MyCustomTableView.h中:

@protocol MyCustomTableViewDataProvider <NSObject>
- (NSArray*)dataArray;
@end

@interface MyCustomTableView : UITableView
@property (nonatomic, weak) id<MyCustomTableViewDataProvider> dataProvider;
@end

The viewController should implement this protocol: viewController应该实现以下协议:

- (NSArray*)dataArray {
    return self.dataArray;
}

And in MyCustomTableView.m I use it like: 在MyCustomTableView.m中,我可以像这样使用它:

NSArray* dataArray = [self.dataProvider dataArray];

second way 第二种方式

in MyCustomTableView.h: 在MyCustomTableView.h中:

@interface MyCustomTableView : UITableView
@property (nonatomic, weak) NSArray* dataArray;
@end

Every time when there's a change in data, the viewController should inform the custom TableView to update its data: 每当数据发生变化时,viewController应该通知自定义TableView更新其数据:

self.customTableView.dataArray = self.dataArray;

And in MyCustomTableView.m I use it like: 在MyCustomTableView.m中,我可以像这样使用它:

self.dataArray

It seems that the second way could save some code, but I heard that you shouldn't store data in views because it violates the MVC principle. 似乎第二种方法可以保存一些代码,但是我听说您不应该在视图中存储数据,因为它违反了MVC原理。 Could you please give me some advice? 你能给我一些建议吗? Many thanks! 非常感谢!

IMHO, I wouldn't subclass any UIView unless it's absolutely necessary. 恕我直言,除非绝对必要,否则我不会继承任何UIView。 Both TableViews and Collections provide protocols to control their behavior correctly. TableViews和Collections均提供协议以正确控制其行为。

If I were you, in order to keep the ViewController clean and short, I would create a custom NSObject implementing both UITableViewDelegate and DataSource protocols. 如果您是我,为了保持ViewController简洁明了,我将创建一个自定义NSObject,同时实现UITableViewDelegate和DataSource协议。 This class would be in charge of providing the style of cells and sections (that is shared in those 2 table views) and also the data source. 此类将负责提供单元格和节的样式(在这两个表视图中共享)以及数据源。 Of course, this object must have a property referencing its TableView. 当然,该对象必须具有引用其TableView的属性。

The ViewController would only be in charge of triggering data retrieval whenever is needed. ViewController仅在需要时负责触发数据检索。 That data should be passed to that custom NSObject, which would be in charge of updating the table view. 该数据应传递到该自定义NSObject,后者将负责更新表视图。

BTW, this object could even be added through InterfaceBuilder. 顺便说一句,甚至可以通过InterfaceBuilder添加该对象。

It's a bit old, I know, but you could check : Lighter VC It's just a step forward towards MVVM. 我知道它有些旧,但是您可以检查一下:更轻便的VC这只是迈向MVVM的一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM