UITableview 优化

目的就是简化代码:


将基本设置封装起来,将需要自定义的方案用代理到外部实现:



类DYTableDelegate,如下:

.h

//
//  DYTableDelegate.h
//  DYTableDelegate
//
//  Created by bqYang on 2017/8/22.
//  Copyright © 2017年 drinye. All rights reserved.
//

#import 
#import 
@interface DYTableDelegate : NSObject
{
    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


.m:

//
//  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



你可能感兴趣的:(应用编程)