zoukankan      html  css  js  c++  java
  • TableView的封装

    1、基于NSObject封装TableView

    TableViewDelegateObj.h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    typedef void (^selectCell) (NSIndexPath *indexPath);
    /**
     *  代理对象(UITableView的协议需要声明在.h文件中,不然外界在使用的时候会报黄色警告,看起来不太舒服)
     */
    @interface TableViewDelegateObj : NSObject <UITableViewDelegate, UITableViewDataSource>
    
    
    /**
     *  创建代理对象实例,并将数据列表传进去
     *  代理对象将消息传递出去,是通过block的方式向外传递消息的
     *  @return 返回实例对象
     */
    + (instancetype)createTableViewDelegateWithDataList:(NSArray *)dataList
                                            selectBlock:(selectCell)selectBlock;
    @end
    

     TableViewDelegateObj.m

    #import "TableViewDelegateObj.h"
    
    @interface TableViewDelegateObj ()
    @property (nonatomic, strong) NSMutableArray   *dataList;
    @property (nonatomic, copy)   selectCell selectBlock;
    @end
    
    @implementation TableViewDelegateObj
    
    + (instancetype)createTableViewDelegateWithDataList:(NSMutableArray *)dataList
                                            selectBlock:(selectCell)selectBlock {
        return [[[self class] alloc] initTableViewDelegateWithDataList:dataList
                                                           selectBlock:selectBlock];
    }
    
    - (instancetype)initTableViewDelegateWithDataList:(NSMutableArray *)dataList selectBlock:(selectCell)selectBlock {
        self = [super init];
        if (self) {
            self.dataList = dataList;
            self.selectBlock = selectBlock;
        }
        return self;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = self.dataList[indexPath.row];
        return cell;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.dataList.count;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        // 将点击事件通过block的方式传递出去
        self.selectBlock(indexPath);
    }
    

     2、 Controller调用

    #import "ViewController.h"
    #import "TableViewDelegateObj.h"
    @interface ViewController ()
    @property (strong, nonatomic) NSMutableArray * arr_test;
    @property (strong, nonatomic) TableViewDelegateObj * tbd_obj;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
        
        self.arr_test = [NSMutableArray arrayWithObjects:@"索马里猫",@"土耳其梵猫",@"美国短毛猫", nil];
        self.tbd_obj = [TableViewDelegateObj createTableViewDelegateWithDataList:self.arr_test
                                                                     selectBlock:^(NSIndexPath *indexPath) {
            NSLog(@"第%@行",indexPath);
        }];
        self.tb_test.delegate = self.tbd_obj;
        self.tb_test.dataSource = self.tbd_obj;
        
        //增加延时看下效果
        [self performSelector:@selector(reloadDataFromNewREquest) withObject:nil afterDelay:2.0];
        
    }
    
    - (void)reloadDataFromNewREquest
    {
        [self.arr_test addObject:@"非洲薮猫"];
        [self.tb_test reloadData];
    }
    
    - (NSMutableArray *)arr_test
    {
        if (!_arr_test) {
            _arr_test = [[NSMutableArray alloc]init];
        }
        return _arr_test;
    }
    

    参考地址:http://www.cocoachina.com/ios/20160317/15696.html

  • 相关阅读:
    关于工作习惯的一点思考
    BulkSqlCopy 批量导入数据(Ef支持)
    记录下最近项目中常用到的SQL语句
    对象化前端表单(Form)提交
    Python描述符 (descriptor) 详解
    Python装饰器之 property()
    Python魔法方法之属性访问 ( __getattr__, __getattribute__, __setattr__, __delattr__ )
    Python魔法方法总结及注意事项
    面向对象编程(二)
    面向对象编程(一)
  • 原文地址:https://www.cnblogs.com/superbobo/p/5286469.html
Copyright © 2011-2022 走看看