iOS 給 ViewController 減負(fù) 之 UITableView
今天看了一些博客文章分享了如何給ViewController 瘦身的問題, 其中一個就是tableView.
的確,隨著產(chǎn)品迭代,VC里面可能越來越臃腫,有時候真的需要好好進(jìn)行一次瘦身.可能是參考的博客中講解更側(cè)重于方法的復(fù)用,其實(shí)這個真的是很靈活的問題,有時候tableView list 都是同一規(guī)格就比較適合可復(fù)用的方法去實(shí)現(xiàn),有時候就是需要單獨(dú)自定義的,一個tableView 可能有不止兩到三個cell 類型的情況. 所以針對后者.我也仿照著寫了一個 把 UITableViewDataSource UITableViewDelegate 都提取出來單獨(dú)做一個對象做處理.
需求:一個tableView 里面目前元素是 一行banner 一行時間 若干行l(wèi)ist 元素組成
效果:

SectionDemo 如下:
// // ViewController減負(fù).h // 鏈?zhǔn)紻SL學(xué)習(xí) // // Created by HF on 17/3/8. // Copyright ? 2017年 HF-Liqun. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController__ : UIViewController @property (nonatomic, strong) UITableView *tableView; @end // // ViewController減負(fù).m // 鏈?zhǔn)紻SL學(xué)習(xí) // // Created by HF on 17/3/8. // Copyright ? 2017年 HF-Liqun. All rights reserved. // #import "ViewController減負(fù).h" #import "HomeBannerCell.h" #import "HomeTableViewDataSource.h" @interface ViewController__ () @property (nonatomic, strong) HomeTableViewDataSource *tabbleViewDataSource; @end @implementation ViewController__ - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view); make.left.right.top.equalTo(self.view); }]; //創(chuàng)建實(shí)例類tabbleViewDataSource //傳入數(shù)據(jù) 這個可以根據(jù)實(shí)際情況靈活處理 self.tabbleViewDataSource = [[HomeTableViewDataSource alloc] initWithItems:@[@"好好",@"學(xué)習(xí)",@"天天",@"向上"] ]; //設(shè)置tableView 兩個代理 self.tableView.dataSource = self.tabbleViewDataSource; self.tableView.delegate = self.tabbleViewDataSource; //每個cell 行 點(diǎn)擊觸發(fā) response TableViewCellBannerAction bannnerAction = ^(id sender) { NSLog(@"%@",sender); }; TableViewCellDailyAction dailyAction = ^(id sender) { NSLog(@"%@",sender); }; TableViewCellListAction listAction = ^(id sender) { NSLog(@"%@",sender); }; self.tabbleViewDataSource.banneryAction = bannnerAction; self.tabbleViewDataSource.dailyAction = dailyAction; self.tabbleViewDataSource.listAction = listAction; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.backgroundColor = [UIColor clearColor]; //_tableView.dataSource = self; // _tableView.delegate = self; // _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[HomeBannerCell class] forCellReuseIdentifier:kHomeBannerCellID]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"]; } return _tableView; } @end
接下來是具體實(shí)施的HomeTableViewDataSource 類
// // HomeTableViewDataSource.h // 鏈?zhǔn)紻SL學(xué)習(xí) // // Created by HF on 17/3/8. // Copyright ? 2017年 HF-Liqun. All rights reserved. // #import <Foundation/Foundation.h> typedef void (^TableViewCellBannerAction)(id sender); typedef void (^TableViewCellDailyAction)(id sender); typedef void (^TableViewCellListAction)(id sender); @interface HomeTableViewDataSource : NSObject <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, copy) TableViewCellBannerAction banneryAction; @property (nonatomic, copy) TableViewCellDailyAction dailyAction; @property (nonatomic, copy) TableViewCellListAction listAction; /** 傳入數(shù)據(jù)源 @param anItems anItems @return anItems */ - (id)initWithItems:(NSArray *)anItems; @end // // HomeTableViewDataSource.m // 鏈?zhǔn)紻SL學(xué)習(xí) // // Created by HF on 17/3/8. // Copyright ? 2017年 HF-Liqun. All rights reserved. // #import "HomeTableViewDataSource.h" #import "HomeBannerCell.h" typedef NS_ENUM(NSUInteger, HomeSectionType) { HomeSectionTypeBanner, //banner HomeSectionTypeDaily, //date HomeSectionTypeList //List }; @interface HomeTableViewDataSource () { NSMutableArray *sectionInfo; } @property (nonatomic, strong) NSArray *items; @end @implementation HomeTableViewDataSource - (id)init { return nil; } - (id)initWithItems:(NSArray *)anItems { self = [super init]; if (self) { sectionInfo = [NSMutableArray array]; [sectionInfo addObject:@(HomeSectionTypeBanner)]; [sectionInfo addObject:@(HomeSectionTypeDaily)]; if (anItems.count > 0) { [sectionInfo addObject:@(HomeSectionTypeList)]; //list 列表 } self.items = anItems; } return self; } - (id)itemAtIndexPath:(NSIndexPath *)indexPath { return self.items[(NSUInteger) indexPath.row]; } #pragma mark UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return sectionInfo.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) { return 1; } if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) { return 1; } if (HomeSectionTypeList == [sectionInfo[section] integerValue]) { return self.items.count; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger section = indexPath.section; if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) { HomeBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomeBannerCellID forIndexPath:indexPath]; return cell; } if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%@",[NSDate date]]; cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; cell.textLabel.textAlignment = NSTextAlignmentCenter; return cell; } if (HomeSectionTypeList == [sectionInfo[section] integerValue]) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath]; id item = [self itemAtIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%@",item]; return cell; } return [UITableViewCell new]; } #pragma mark UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger section = indexPath.section; if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) { return [HomeBannerCell getCellHeight]; } if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) { return 55; } if (HomeSectionTypeList == [sectionInfo[section] integerValue]) { return 44; } return 0; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger section = indexPath.section; if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) { if (self.banneryAction) { self.banneryAction(@"點(diǎn)擊了banner"); } } if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) { if (self.dailyAction) { self.dailyAction(@"點(diǎn)擊了日期"); } } if (HomeSectionTypeList == [sectionInfo[section] integerValue]) { if (self.listAction) { id item = [self itemAtIndexPath:indexPath]; self.listAction([NSString stringWithFormat:@"點(diǎn)擊了list單元 %@",item]); } } } @end
參考:
1.https://objccn.io/issue-1-1/
posted on 2017-03-10 15:55 ACM_Someone like you 閱讀(544) 評論(0) 收藏 舉報
浙公網(wǎng)安備 33010602011771號