标签:
原本项目页面不是由TableView构成,但是我用tableView比较适合,所以重写了一边,
项目源代码可以从我的gitHub https://github.com/vfanx/FreeLimit 上获得,这里我将tableView拆分成三部分
将DataSource和tableView分离,使其可以复用,抽象出部分接口使其可以定制
在这里,我一个创建了两个DataSource类,下面我贴出这个页面的DataSource类
1 #import <UIKit/UIKit.h> 2 #import "AppModel.h" 3 #import "XTableCellConfig.h" 4 5 @interface XDetailViewDataSource : NSObject<UITableViewDataSource,UITableViewDelegate> 6 7 - (instancetype)initWithTableView:(UITableView *)tableview 8 dataModel:(AppModel *)model 9 configureCellArry:(NSArray *)configureArry; 10 11 @end 12 13 14 15 #import "XDetailViewDataSource.h" 16 17 @interface XDetailViewDataSource () 18 19 @property (nonatomic,weak) UITableView *tableView; 20 @property (nonatomic,strong) AppModel *model; 21 @property (nonatomic,strong) NSArray *configureArry; 22 23 @end 24 25 @implementation XDetailViewDataSource 26 27 - (instancetype)initWithTableView:(UITableView *)tableview 28 dataModel:(AppModel *)model 29 configureCellArry:(NSArray *)configureArry 30 { 31 self = [super init]; 32 if (self) { 33 _tableView = tableview; 34 _configureArry = configureArry; 35 _model = model; 36 } 37 return self; 38 } 39 40 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 41 { 42 return _configureArry.count; 43 } 44 45 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 46 { 47 return [(NSArray *)_configureArry[section] count]; 48 } 49 50 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 51 { 52 XTableCellConfig *cellConfig = _configureArry[indexPath.section][indexPath.row]; 53 UITableViewCell *cell = nil; 54 cell = [cellConfig cellOfCellConfigWithTableView:tableView dataModel:_model isNib:YES]; 55 cell.selectionStyle = UITableViewCellSelectionStyleNone; 56 return cell; 57 } 58 59 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 60 { 61 XTableCellConfig *cellConfig = _configureArry[indexPath.section][indexPath.row]; 62 return cellConfig.heightOfCell; 63 } 64 65 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 66 { 67 return @" "; 68 } 69 70 @end
这个类存储着每个cell是怎么配置的
#import <UIKit/UIKit.h> @interface XTableCellConfig : NSObject @property (nonatomic, copy) NSString *className; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) void(^showInfoBlock)(id cell,id model); @property (nonatomic, assign) CGFloat heightOfCell; @property (nonatomic, strong) NSString *detail; @property (nonatomic, strong) NSString *remark; + (instancetype)cellConfigWithClassName:(NSString *)className title:(NSString *)title showInfoBlock:(void(^)(id cell,id model))showInfoMethod heightOfCell:(CGFloat)heightOfCell; - (UITableViewCell *)cellOfCellConfigWithTableView:(UITableView *)tableView dataModel:(id)dataModel; - (UITableViewCell *)cellOfCellConfigWithTableView:(UITableView *)tableView dataModel:(id)dataModel isNib:(BOOL)isNib; @end #import "XTableCellConfig.h" @implementation XTableCellConfig + (instancetype)cellConfigWithClassName:(NSString *)className title:(NSString *)title showInfoBlock:(void(^)(id cell,id model))showInfoBlock heightOfCell:(CGFloat)heightOfCell { XTableCellConfig *cellConfig = [XTableCellConfig new]; cellConfig.className = className; cellConfig.title = title; cellConfig.showInfoBlock = showInfoBlock; cellConfig.heightOfCell = heightOfCell; return cellConfig; } - (UITableViewCell *)cellOfCellConfigWithTableView:(UITableView *)tableView dataModel:(id)dataModel { return [self cellOfCellConfigWithTableView:tableView dataModel:dataModel isNib:NO]; } - (UITableViewCell *)cellOfCellConfigWithTableView:(UITableView *)tableView dataModel:(id)dataModel isNib:(BOOL)isNib { Class cellClass = NSClassFromString(self.className); NSString *cellID = self.className; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { if (isNib) { NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:self.className owner:nil options:nil]; cell = [nibs lastObject]; } else { cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]; } } if (self.showInfoBlock) { _showInfoBlock(cell,dataModel); } return cell; } @end
创建Datasource,cellConfigure,并与其关联即可
具体参考代码:
// // DetailViewController.m // FreeLimit // // Created by TBXark on 15-4-9. // Copyright (c) 2015年 TBXark. All rights reserved. // #import "DetailViewController.h" #import "UIKit+AFNetworking.h" #import "XTableCellConfig.h" #import "AppInfoTableViewCell.h" #import "MultButtonTableViewCell.h" #import "PhotoScrolTableViewCell.h" #import "DetailTextTableViewCell.h" #import "XDetailViewDataSource.h" #import "AppPicViewController.h" @interface DetailViewController ()<UITableViewDelegate> @property (nonatomic,strong) NSMutableArray *cellConfigureArry; @property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) XDetailViewDataSource *dataSource; @end @implementation DetailViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"应用详情"; self.view.backgroundColor = [UIColor whiteColor]; _cellConfigureArry = [[NSMutableArray alloc] init]; [self createTopView]; [self createButtomView]; [self createTableView]; } #pragma mark - Create TableView - (void)createTableView { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:_tableView]; _dataSource = [[XDetailViewDataSource alloc] initWithTableView:_tableView dataModel:_model configureCellArry:_cellConfigureArry]; _tableView.dataSource = _dataSource; _tableView.delegate = _dataSource; } #pragma mark - Create Top View - (void)createTopView { NSMutableArray *topViewCongureArry = [[NSMutableArray alloc] init]; typeof(self) __weak weakSelf = self; XTableCellConfig *appInfo = [XTableCellConfig cellConfigWithClassName:NSStringFromClass([AppInfoTableViewCell class]) title:@"AppInfo" showInfoBlock:^(AppInfoTableViewCell *cell,AppModel *model) { [cell.iconImage setImageWithURL:[NSURL URLWithString:model.iconUrl]]; cell.iconImage.clipsToBounds = YES; cell.nameLable.text = model.name; cell.priceLable.text = [NSString stringWithFormat:@"原价 : $ %.2f",model.lastPrice.doubleValue]; cell.sizeLable.text = [NSString stringWithFormat:@"类型 : %@",_model.categoryName]; cell.categoryLable.text = [NSString stringWithFormat:@"类型 : %@",_model.categoryName]; cell.starLable.text = [NSString stringWithFormat:@"评分 : %.2f",_model.starCurrent.doubleValue]; } heightOfCell:AppInfoTableViewCellHeight]; [topViewCongureArry addObject:appInfo]; XTableCellConfig *multButton = [XTableCellConfig cellConfigWithClassName:NSStringFromClass([MultButtonTableViewCell class]) title:@"MultButton" showInfoBlock:^(MultButtonTableViewCell *cell, AppModel *model) { NSArray *tittleArry = @[@"分享 ",@"收藏 ",@"下载 "]; [cell.leftButton setTitle:tittleArry[0] forState:UIControlStateNormal]; [cell.centerButton setTitle:tittleArry[1] forState:UIControlStateNormal]; [cell.rightButton setTitle:tittleArry[2] forState:UIControlStateNormal]; [cell.rightButton setAction:^(UIButton *button){ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:model.itunesUrl]]; }]; [cell.leftButton setAction:^(UIButton *button){ [weakSelf cellButtonClick:@"Share"]; }]; [cell.centerButton setAction:^(UIButton *button){ [weakSelf cellButtonClick:@"Favourite"]; }]; } heightOfCell:MultButtonTableViewCellHeight]; [topViewCongureArry addObject:multButton]; XTableCellConfig *photoScroll = [XTableCellConfig cellConfigWithClassName:NSStringFromClass([PhotoScrolTableViewCell class]) title:@"AppPhoto" showInfoBlock:^(PhotoScrolTableViewCell *cell, id model) { cell.model = model; cell.isSmallPic = YES; [cell starDownDetailDataInfo]; [cell setAppPicIsTapWithIndex:^(NSInteger index) { NSLog(@"I am here"); AppPicViewController *appPic = [[AppPicViewController alloc] init]; appPic.model = model; appPic.index = index; [weakSelf.navigationController pushViewController:appPic animated:YES]; }]; } heightOfCell:PhotoScrolTableViewCellHeight]; [topViewCongureArry addObject:photoScroll]; XTableCellConfig *detailText = [XTableCellConfig cellConfigWithClassName:NSStringFromClass([DetailTextTableViewCell class]) title:@"Description" showInfoBlock:^(DetailTextTableViewCell *cell,AppModel *model) { NSString *changeBr = [model.descriptionStr stringByReplacingOccurrencesOfString:@"<br />" withString:@" "]; cell.detailLable.text = changeBr; } heightOfCell:60]; [topViewCongureArry addObject:detailText]; [_cellConfigureArry addObject:topViewCongureArry]; } - (void)createButtomView { NSMutableArray *buttomViewCongureArry = [[NSMutableArray alloc] init]; XTableCellConfig *nearApp = [XTableCellConfig cellConfigWithClassName:NSStringFromClass([PhotoScrolTableViewCell class]) title:@"NearApp" showInfoBlock:^(PhotoScrolTableViewCell *cell, id model) { cell.model = model; [cell starDownLoadNearbyData]; [cell setAppIconIsTapWithIndex:^(AppModel *model) { DetailViewController *dvc = [[DetailViewController alloc] init]; dvc.model = model; [self.navigationController pushViewController:dvc animated:YES]; }]; } heightOfCell:PhotoScrolTableViewCellHeight]; [buttomViewCongureArry addObject:nearApp]; [_cellConfigureArry addObject:buttomViewCongureArry]; } - (void)topViewButtonClick:(id)sender { UIButton *button = sender; switch (button.tag) { case 103: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_model.itunesUrl]]; break; default: break; } } - (void)cellButtonClick:(NSString *)str { UIAlertView *ale = [[UIAlertView alloc] initWithTitle:str message:@"Test Button Action" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [ale show]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
语言组织能力不太好,所以能用代码表达的,就比较少打字表达,上面使用到的方法,可以极大的提高代码的复用性;
具体可以参考github上的完整代码,写这篇博文时项目没有完成,也不知道什么时候完成,但是已经是现实上述方法.
标签:
原文地址:http://www.cnblogs.com/vfanx/p/4411721.html