iOS 瘦身ViewController 分离tableViewDataSource
Posted ljbguanli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 瘦身ViewController 分离tableViewDataSource相关的知识,希望对你有一定的参考价值。
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;
[email protected]"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的主要内容,如果未能解决你的问题,请参考以下文章