Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ALGenericTableViewDataSource.m
  3. //  AddisonLeeiOS
  4. //
  5. //  Created by Leonid Grebeniuk on 7/14/16.
  6. //  Copyright © 2016 DMI. All rights reserved.
  7. //
  8.  
  9. #import "ALGenericTableViewDataSource.h"
  10.  
  11. @implementation ALGenericTableViewDataSource
  12.  
  13. - (instancetype)initWithData:(NSArray *)dataArray
  14. {
  15.   self = [super init];
  16.   if (self) {
  17.     self.dataArray = dataArray;
  18.   }
  19.   return self;
  20. }
  21.  
  22. #pragma mark - UITableViewDataSource
  23. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  24.   return [self.dataArray count];
  25. }
  26.  
  27. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  28.   return [self.dataArray[section] count];
  29. }
  30.  
  31. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  32.   NSDictionary *dataCell = [self dataAtIndexPath:indexPath];
  33.   UITableViewCell <ALCellProtocol> *cell = [tableView dequeueReusableCellWithIdentifier:dataCell[kKeyClassCellName]
  34.                                                                            forIndexPath:indexPath];
  35.   if (self.delegate != nil && [cell respondsToSelector:@selector(setDelegate:)]) {
  36.     cell.delegate = self.delegate;
  37.   }
  38.  
  39.   [cell populateCellWithData:dataCell];
  40.   return cell;
  41. }
  42.  
  43. #pragma mark - Helper
  44. - (NSDictionary *)dataAtIndexPath:(NSIndexPath *)indexPath {
  45.   NSArray *sectionArray = self.dataArray[indexPath.section];
  46.   NSMutableDictionary *dataCell = [NSMutableDictionary dictionaryWithDictionary:sectionArray[indexPath.row]];
  47.   [dataCell setObject:indexPath forKey:kKeyIndexPath];
  48.  
  49.   return dataCell;
  50. }
  51.  
  52. - (NSInteger)cellTypeAtIndexPath:(NSIndexPath *)indexPath {
  53.   if (self.dataArray.count < indexPath.section ) {
  54.     return -1;
  55.   }
  56.   NSArray *rowArray = self.dataArray[indexPath.section];
  57.   NSDictionary *dataCell = [NSMutableDictionary dictionaryWithDictionary:rowArray[indexPath.row]];
  58.  
  59.   return dataCell[kKeyCellType]?  [dataCell[kKeyCellType] integerValue] : -1;
  60. }
  61.  
  62. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement