2017-10-04 34 views
0

我在设计时考虑到可重用性时遇到了一些麻烦。在我的应用程序的一部分,我有一个UITableViewController和另一个我有一个UITableView。这两个应该看起来是一样的,他们使用相同的数据。这就是为什么我认为我应该开始使用单独的UITableViewDelegateUITableViewDatasource类。如何设计tableview重用?

@interface RestaurantsTableSource : NSObject <UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, strong) RLMResults *restaurants; 

@end 

在我UIViewController我会再有一个强大的参考这个:

@interface RestaurantsViewController() 

@property (nonatomic, strong) RestaurantsTableSource *source; 

@end 

,并设置为使用的实现代码如下:

self.tableView.dataSource = self.source; 
self.tableView.delegate = self.source; 

然后,当数据变化,我会重新分配到RestaurantsTableSource

self.source.restaurants = [Database getRestaurants]; // Or something like it 
[self.tableView reloadData]; 

但是,我不知道这是否是处理这种事情的最佳方式。你对此有何评论?

回答

0

我同意你的看法,直到这些线

self.tableView.dataSource = self.source; 
self.tableView.delegate = self.source; 

这些行后,我更愿意称之为数据源检索数据。

- (void) tableView:(UITableView *)tableView loadPage:(NSUInteger)page { 
    // Implement your server call or local call and finally reloadData 
    [tableView reloadData]; 
} 
:在你RestaurantsTableSource类这样的事情

[self.source tableView:self.tableView loadPage:1]; 

和实施本次公开方法