<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      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)    收藏  舉報

      導(dǎo)航

      主站蜘蛛池模板: 亚洲中文字幕人妻系列| 蜜桃av一区二区高潮久久精品| 成 人 免费 在线电影| 国产果冻豆传媒麻婆| 国产亚洲精品97在线视频一| 婷婷丁香五月亚洲中文字幕| 亚洲天堂av日韩精品| 人妻聚色窝窝人体WWW一区| 亚洲a∨国产av综合av| 亚洲精品自拍在线视频| 亚洲精品日本久久久中文字幕| 福利一区二区在线视频| 大陆熟妇丰满多毛xxxⅹ| 日韩精品一区二区三区在线观看 | 亚洲国产理论片在线播放| 日本高清视频网站www| 午夜福利国产精品小视频| 欧美黑人XXXX性高清版| 亚洲无av在线中文字幕| 国产精品 亚洲一区二区三区| 色综合久久人妻精品日韩| 九九日本黄色精品视频| 风韵丰满熟妇啪啪区老熟熟女 | 激情四射激情五月综合网| 岛国岛国免费v片在线观看| 在线无码免费看黄网站| 青青草原国产精品啪啪视频| 久久精产国品一二三产品| 日韩中文字幕精品人妻| 久久91精品牛牛| 成人视频在线观看| 免费无码AV一区二区波多野结衣 | 亚洲色av天天天天天天| 国产成人午夜精品影院| 亚洲高清乱码午夜电影网| 国产成人久久777777| 亚洲a∨无码无在线观看| 国产第一区二区三区精品| 4hu四虎永久在线观看| 日本黄漫动漫在线观看视频| 麻豆国产va免费精品高清在线|