UITableview 优化
来源:互联网 发布:百事通酒店软件 编辑:程序博客网 时间:2024/06/05 19:25
目的就是简化代码:
将基本设置封装起来,将需要自定义的方案用代理到外部实现:
类DYTableDelegate,如下:
.h
//// DYTableDelegate.h// DYTableDelegate//// Created by bqYang on 2017/8/22.// Copyright © 2017年 drinye. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface DYTableDelegate : NSObject<UITableViewDataSource,UITableViewDelegate>{ void(^cellClick)(NSIndexPath* indexpath); void(^cellConfig)(id cell,id item); void(^cellEdit)(id cell,id item); CGFloat(^cellHieght)(id cell,id item); UITableView* tabview; NSString* cellIdentifierName; BOOL isMultiSection; BOOL isCellEdit; Class cellClass; CGFloat mCellHeight; }@property(nonatomic,strong) id dataArr;-(id)initWithTableView: (UITableView*)tableview cellClass:(Class)rowClass cellIndentifier:(NSString*)cellIdentifier useXib:(BOOL)useXib height:(CGFloat)rowHeight data:(id)data multiSection:(BOOL)isMulti canScroll:(BOOL)isScroll isEdit:(BOOL)isEdit click:(void(^)(NSIndexPath* indexpath))rowClick config:(void(^)(id cell,id item))rowConfig edit:(void(^)(id cell,id item))rowEdit height:(CGFloat(^)(id cell,id item))rowHeight;@end
//// DYTableDelegate.m// DYTableDelegate//// Created by bqYang on 2017/8/22.// Copyright © 2017年 drinye. All rights reserved.//#import "DYTableDelegate.h"@implementation DYTableDelegate@synthesize dataArr;-(id)initWithTableView:(UITableView *)tableview cellClass:(Class)rowClass cellIndentifier:(NSString *)cellIdentifier useXib:(BOOL)useXib height:(CGFloat)mrowHeight data:(id)data multiSection:(BOOL)isMulti canScroll:(BOOL)isScroll isEdit:(BOOL)isEdit click:(void (^)(NSIndexPath *))rowClick config:(void (^)(id, id))rowConfig edit:(void (^)(id, id))rowEdit height:(CGFloat(^)(id cell,id item))rowHeight{ if(self = [super init]){ tabview= tableview; tabview.delegate = self; tabview.dataSource = self; [tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone]; [tableview setScrollEnabled:isScroll]; [tableview setShowsVerticalScrollIndicator:NO]; cellIdentifierName = cellIdentifier; if(rowClass){ cellClass = rowClass; }else{ return NULL; } [tabview registerClass:cellClass forCellReuseIdentifier:cellIdentifierName]; if(rowConfig) cellConfig = rowConfig; if(rowClick) cellClick = rowClick; if(rowEdit) cellEdit = rowEdit; if(rowHeight) cellHieght = rowHeight; if(data){ dataArr = data; } isMultiSection = isMulti; mCellHeight = mrowHeight; isCellEdit = isEdit; if(useXib){ [tabview registerNib:[UINib nibWithNibName:cellIdentifierName bundle:nil] forCellReuseIdentifier:cellIdentifierName]; } } return self;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ NSInteger num=1; if(isMultiSection){ num = [dataArr count]; } return num;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat height = mCellHeight;// if(!(cellClass == [UITableViewCell class])){// height+=50;//change your code// } if(mCellHeight==0){ UITableViewCell *cell = [[cellClass alloc]init]; id item = NULL; if(isMultiSection) item = dataArr[indexPath.section][indexPath.row]; else item=dataArr[indexPath.row];// height = cellHeight(cell,item); } return height;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSInteger num = 1; if(isMultiSection){ num = [dataArr[section] count]; }else{ num = [dataArr count]; } return num;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierName]; if(cell == nil){ cell = [[cellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierName]; } [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; id item = NULL; if(isMultiSection) item = dataArr[indexPath.section][indexPath.row]; else item=dataArr[indexPath.row]; cellConfig(cell,item); return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:NO]; cellClick(indexPath);}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return isCellEdit;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ id item = NULL; if(isMultiSection) item = dataArr[indexPath.section][indexPath.row]; else item=dataArr[indexPath.row]; cellEdit(indexPath,item);}-(void)setDataArr:(id)dtArr{ dataArr =dtArr; [tabview reloadData];}@end
在viewcontroller中使用:
#import "ViewController.h"#import "DYTableDelegate.h"@interface ViewController ()@property (strong,nonatomic) UITableView* tableview;@property (strong,nonatomic) DYTableDelegate* delegate;@end@implementation ViewController@synthesize tableview,delegate;-(void)viewWillAppear:(BOOL)animated{ }- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *arr1 = @[@"行-111",@"行-222",@"行-333"]; tableview= [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 300, 400) style:UITableViewStyleGrouped]; [self.view addSubview:tableview]; delegate = [[DYTableDelegate alloc]initWithTableView:tableview cellClass:NULL cellIndentifier:@"cellTest" useXib:NO height:80 data:arr1 multiSection:NO canScroll:(BOOL)NO isEdit:YES click:^(NSIndexPath *indexpath){ NSLog(@"click %ld row",indexpath.row); } config:^(id cell, id item) { UITableViewCell* mycell =cell; NSString* str = item; [mycell.textLabel setText:str]; } edit:^(id cell,id item){ } height:^CGFloat(id cell, id item) { return 40; }]; }@end
阅读全文
0 0
- UITableView优化
- UITableView优化
- UItableView优化
- UITableView 优化
- UITableView优化
- UITableView优化
- UITableView优化
- UITableView优化
- UITableView优化
- UITableView优化
- UITableView优化
- UITableVIew优化
- UITableView优化
- UITableview 优化
- UITableView 优化
- 优化UITableView性能
- 如何优化UITableView性能
- 优化UITableView性能
- 敏捷过程与极限编程(XP)
- Python和Pygame游戏开发指南(1)MOMERY PUZZLE
- Shader学习之Cg语言五(Cg函数与标准函数库)
- HTML5
- HDU
- UITableview 优化
- Java 面试常见问题
- service层深入理解
- jQuery对的表单数据序列化和校验
- 神经网络中的优化算法
- QtCharts动态的显示折线图
- 无卡下游商户交易流程transInit()
- rman RAC到单机
- 3Sum Closest 三个数的和最接近某个值