为了账号安全,请及时绑定邮箱和手机立即绑定

iOS学习笔记--UITableView (二)

标签:
iOS

UITableView笔记二

2016-03-17 18:24:48

前一段时间看了一篇文章,讲的是MVC模式,其中就有任务分担原则,不要所有的事情都让控制器去做,MVC模式下把M层负责处理数据 View层负责反馈交互和绘制UI ,而C层就是协调UI和数据之间的关系,Controller原本是比较简洁的完成任务的,而许多人也包括之前的我都把任务加到Controller层,这对性能来说是一种不均衡的压力,要想实现代码块的高重用性,就要降低MVC之间的耦合,做到低耦合、高内聚。咱们以UITableView为例,把UITableView的代理提出来,已达到Controller的可重用能力。

<!---more--->
这是我封装的TableViewDelegate类,专门负责处理数据和点击回调

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^CellSelectedBlock) (NSIndexPath *indexpath);
@interface TableViewDelegate : NSObject<UITableViewDataSource,UITableViewDelegate>

+ (instancetype)creatAndSetTableViewDelegateObjctWithData:(NSArray *)dataArr cellSelectedBlock:(CellSelectedBlock)selectedBlock;

@end

.m具体实现


#import "TableViewDelegate.h"
#import "NewTableViewCell.h"
@interface TableViewDelegate()
////代理数据源
@property (nonatomic, strong)NSArray *dataArr;

@property (nonatomic, copy)CellSelectedBlock selectedBlock;

@end

@implementation TableViewDelegate

+ (instancetype)creatAndSetTableViewDelegateObjctWithData:(NSArray *)dataArr cellSelectedBlock:(CellSelectedBlock)selectedBlock
{
    return [[self alloc]initTableViewDelegateWithDataArray:dataArr cellSlectedBlock:selectedBlock];
}
- (instancetype)initTableViewDelegateWithDataArray:(NSArray *)dataArr cellSlectedBlock:(CellSelectedBlock)selectedBlock
{
    self = [super init];
    if (self) {
        self.dataArr = dataArr;
        self.selectedBlock = selectedBlock;
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewTableViewCellId];///在tableView的重用队列里面取带有identifier标记的cell,也就是目标cell
    if (!cell) {///如果重用池(重用队列)里没有这种cell
        cell = [[NewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kNewTableViewCellId];///没有目标cell就去初始化新的cell 并制定标记identifier
    }
    cell.textLabel.text = self.dataArr[indexPath.row];///简单的可以直接用系统的cell
    cell.seperateStyle = SeperateStyle_LongWidth;///其实这两步应该交给cell自己处理,由于数据原因写在这里

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectedBlock(indexPath);
}

@end

具体使用

#pragma mark tableView Getter
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _delegate = [TableViewDelegate creatAndSetTableViewDelegateObjctWithData:self.dataArray cellSelectedBlock:^(NSIndexPath *indexpath) {
            NSLog(@"我选中了第%ld行",indexpath.row);
        }];////这个必须要被本控制器持有,否则的话,delegate就被释放掉了
        _tableView.delegate = _delegate;
        _tableView.dataSource = _delegate;
        _tableView.rowHeight = 50;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;///在这里先把系统的cell的分割线关掉
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
32
获赞与收藏
322

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消