iOS 给 ViewController 减负 之 UITableView

  今天看了一些博客文章分享了如何给ViewController 瘦身的问题, 其中一个就是tableView.

  的确,随着产品迭代,VC里面可能越来越臃肿,有时候真的需要好好进行一次瘦身.可能是参考的博客中讲解更侧重于方法的复用,其实这个真的是很灵活的问题,有时候tableView list 都是同一规格就比较适合可复用的方法去实现,有时候就是需要单独自定义的,一个tableView 可能有不止两到三个cell 类型的情况. 所以针对后者.我也仿照着写了一个 把 UITableViewDataSource UITableViewDelegate 都提取出来单独做一个对象做处理.

  需求:一个tableView 里面目前元素是 一行banner  一行时间  若干行list 元素组成

  效果:

  SectionDemo 如下:

 

//
//  ViewController减负.h
//  链式DSL学习
//
//  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减负.m
//  链式DSL学习
//
//  Created by HF on 17/3/8.
//  Copyright © 2017年 HF-Liqun. All rights reserved.
//

#import "ViewController减负.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);
    }];
    
    //创建实例类tabbleViewDataSource
    //传入数据 这个可以根据实际情况灵活处理
    self.tabbleViewDataSource = [[HomeTableViewDataSource alloc]
                                 initWithItems:@[@"好好",@"学习",@"天天",@"向上"] ];
    
    //设置tableView 两个代理
    self.tableView.dataSource = self.tabbleViewDataSource;
    self.tableView.delegate = self.tabbleViewDataSource;
    
    //每个cell 行 点击触发 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

接下来是具体实施的HomeTableViewDataSource 类 

//
//  HomeTableViewDataSource.h
//  链式DSL学习
//
//  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; /** 传入数据源 @param anItems anItems @return anItems */ - (id)initWithItems:(NSArray *)anItems; @end // // HomeTableViewDataSource.m // 链式DSL学习 // // 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(@"点击了banner"); } } if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) { if (self.dailyAction) { self.dailyAction(@"点击了日期"); } } if (HomeSectionTypeList == [sectionInfo[section] integerValue]) { if (self.listAction) { id item = [self itemAtIndexPath:indexPath]; self.listAction([NSString stringWithFormat:@"点击了list单元 %@",item]); } } } @end 

参考:

1.https://objccn.io/issue-1-1/

posted on 2017-03-10 15:55  ACM_Someone like you  阅读(520)  评论(0编辑  收藏  举报

导航