码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 瘦身ViewController 分离tableViewDataSource

时间:2017-06-12 19:42:54      阅读:473      评论:0      收藏:0      [点我收藏+]

标签:string   port   name   frame   self   obj   identify   queue   ogre   


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;

iOS 瘦身ViewController 分离tableViewDataSource

标签:string   port   name   frame   self   obj   identify   queue   ogre   

原文地址:http://www.cnblogs.com/ljbguanli/p/6994384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!