|
| 1 | +// |
| 2 | +// KtBaseTableView.m |
| 3 | +// KtTableView |
| 4 | +// |
| 5 | +// Created by baidu on 16/4/13. |
| 6 | +// Copyright © 2016年 zxy. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "KtBaseTableView.h" |
| 10 | +#import "KtBaseTableViewCell.h" |
| 11 | +#import "KtTableViewSectionObject.h" |
| 12 | + |
| 13 | +@implementation KtBaseTableView |
| 14 | + |
| 15 | +- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style { |
| 16 | + self = [super initWithFrame:frame style:style]; |
| 17 | + if (self) { |
| 18 | + self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 19 | + self.separatorColor = [UIColor clearColor]; |
| 20 | + self.showsVerticalScrollIndicator = YES; |
| 21 | + self.showsHorizontalScrollIndicator = NO; |
| 22 | + self.sectionHeaderHeight = 0; |
| 23 | + self.sectionFooterHeight = 0; |
| 24 | + self.delegate = self; |
| 25 | + } |
| 26 | + return self; |
| 27 | +} |
| 28 | + |
| 29 | +- (void)setKtDataSource:(id<KtTableViewDataSource>)ktDataSource { |
| 30 | + if (_ktDataSource != ktDataSource) { |
| 31 | + _ktDataSource = ktDataSource; |
| 32 | + self.dataSource = ktDataSource; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#pragma mark - UITableViewDelegate |
| 37 | + |
| 38 | +- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath { |
| 39 | + id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource; |
| 40 | + |
| 41 | + KtTableViewBaseItem *object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath]; |
| 42 | + Class cls = [dataSource tableView:tableView cellClassForObject:object]; |
| 43 | + |
| 44 | + return [cls tableView:tableView rowHeightForObject:object]; |
| 45 | +} |
| 46 | + |
| 47 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 48 | + if ([self.ktDelegate respondsToSelector:@selector(didSelectObject:atIndexPath:)]) { |
| 49 | + id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource; |
| 50 | + id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath]; |
| 51 | + [self.ktDelegate didSelectObject:object atIndexPath:indexPath]; |
| 52 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 53 | + } |
| 54 | + else if ([self.ktDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) { |
| 55 | + [self.ktDelegate tableView:tableView didSelectRowAtIndexPath:indexPath]; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
| 61 | + if ([self.ktDelegate respondsToSelector:@selector(headerViewForSectionObject:atSection:)]) { |
| 62 | + id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource; |
| 63 | + KtTableViewSectionObject *sectionObject = [((KtTableViewDataSource *)dataSource).sections objectAtIndex:section]; |
| 64 | + |
| 65 | + return [self.ktDelegate headerViewForSectionObject:sectionObject atSection:section]; |
| 66 | + } |
| 67 | + else if ([self.ktDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) { |
| 68 | + return [self.ktDelegate tableView:tableView viewForHeaderInSection:section]; |
| 69 | + } |
| 70 | + return nil; |
| 71 | +} |
| 72 | + |
| 73 | +#pragma mark - 传递原生协议 |
| 74 | + |
| 75 | +- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { |
| 76 | + if ([self.ktDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)]) { |
| 77 | + [self.ktDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath]; |
| 78 | + } |
| 79 | +} |
| 80 | +@end |
0 commit comments