Skip to content

Instantly share code, notes, and snippets.

@Frankacy
Created November 16, 2016 12:23
Show Gist options
  • Save Frankacy/c9cb9f5612f79d5a366759559886f10e to your computer and use it in GitHub Desktop.
Save Frankacy/c9cb9f5612f79d5a366759559886f10e to your computer and use it in GitHub Desktop.
Table View Adapter
@interface TableViewAdapter : NSObject <FRKDataProvider, UITableViewDataSource>
- (instancetype)initWithDataProvider:(id<FRKDataProvider, FRKSectionInfoProvider>)provider
dataPresenter:(id<FRKTableViewDataPresenter>)presenter;
- (void)registerCellsForTableView:(UITableView *)tableView;
@end
@interface FRKTableViewAdapter ()
@property(nonatomic, strong) id<FRKDataProvider, FRKSectionInfoProvider> provider;
@property(nonatomic, strong) id<FRKTableViewDataPresenter> presenter;
@end
@implementation FRKTableViewAdapter
- (instancetype)initWithDataProvider:(id<FRKDataProvider, FRKSectionInfoProvider>)provider
dataPresenter:(id<FRKTableViewDataPresenter>)presenter {
self = [super init];
if (!self) {
return nil;
}
_provider = provider;
_presenter = presenter;
return self;
}
- (void)registerCellsForTableView:(UITableView *)tableView {
[self.presenter registerCellsForTableView:tableView];
}
#pragma mark - FRKDataProvider
- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
return [self.provider itemAtIndexPath:indexPath];
}
- (NSIndexPath *)indexForItem:(id)item {
return [self.provider indexForItem:item];
}
- (void)loadDataWithCompletion:(void(^)(NSError *error))completion {
[self.provider loadDataWithCompletion:completion];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.provider numberOfSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.provider numberOfItemsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id item = [self itemAtIndexPath:indexPath];
return [self.presenter tableView:tableView cellForItem:item atIndexPath:indexPath];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment