关闭

[置顶] iOS 瘦身ViewController 分离tableViewDataSource

标签: iosViewcontroller瘦身
711人阅读 评论(2) 收藏 举报
分类:


1:

#import <Foundation/Foundation.h>
typedef void(^configureCellBlock)(id cell, id item);//瘦身viewcontroller


@interface RRFriendTableViewDataSource : NSObject<UITableViewDataSource>
- (id)initWithItems:(NSArray *)items cellItentifier:(NSString *)cellIdentify configureCellBlock:(configureCellBlock)cellBlock;

- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end


2:

#import "RRFriendTableViewDataSource.h"
#import "RRFriendViewControllerCell.h"

@interface RRFriendTableViewDataSource ()
@property (nonatomic,strong) NSArray *items;
@property (nonatomic,copy)   NSString *cellIdentify;
@property (nonatomic,copy)   configureCellBlock cellBlock;
@end

@implementation RRFriendTableViewDataSource
- (id)init{
    return nil;
}
- (id)initWithItems:(NSArray *)items cellItentifier:(NSString *)cellIdentify configureCellBlock:(configureCellBlock)cellBlock{
    self=[super init];
    if (self) {
        self.items=items;
        self.cellIdentify=cellIdentify;
        self.cellBlock=cellBlock;
    }
    return self;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath{
    return self.items[(NSUInteger)indexPath.row];
}
#pragma mark - dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        UITableViewCell *cell=[self tableViewZero:tableView cellForRowAtIndexPath:indexPath];
        return cell;
    }
    RRFriendViewControllerCell *cell=[tableView dequeueReusableCellWithIdentifier:self.cellIdentify];
    if (cell==nil) {
        cell=[[RRFriendViewControllerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentify];
    }
    id item=[self itemAtIndexPath:indexPath];
    self.cellBlock(cell,item);
    return cell;
}
- (UITableViewCell *)tableViewZero:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,44)];
    label.textAlignment=NSTextAlignmentCenter;
    label.text=@"xxxxxxxx";
    [cell.contentView addSubview:label];
    cell.backgroundColor=RGBACOLOR(235, 235, 235, 1);
    return cell;
}
@end


3:使用方法

可以把ViewController里面的tableViewDataSource的三个方法给去掉,然后在加载tableview的时候写上,

NSArray *array=[NSArray arrayWithArray:self.cellInfoArray];
    void (^configureCell)(RRFriendViewControllerCell*,RRFriendViewControllerModel *) = ^(RRFriendViewControllerCell* cell,RRFriendViewControllerModel *modelInfo) {
        
        PAImageView *image=[[PAImageView alloc]initWithFrame:cell.myimage.bounds backgroundProgressColor:[UIColor lightGrayColor] progressColor:nil image:nil];
        [cell.myimage addSubview:image];
        [image setImageURL:modelInfo.image];
        cell.mynickName=modelInfo.nickName;
        cell.myID=modelInfo.ID;
        cell.myrunDistance=modelInfo.rundistance;
        cell.mydistance=modelInfo.distance;
    };
    
    self.arrayDataSource=[[RRFriendTableViewDataSource alloc]initWithItems:array cellItentifier:@"RRFriendViewControllerCell" configureCellBlock:configureCell];
    
    self.myTableView.dataSource=self.arrayDataSource;

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:180626次
    • 积分:3627
    • 等级:
    • 排名:第8985名
    • 原创:180篇
    • 转载:8篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论