一说到UITableView这个控件,估计大家都十分熟悉,这是IOS开发当中最常用的控件之一。每次用到UITableView这个控件时都要写好多代码。写起来也比较头疼。代码多起来也不方便维护。减少代码量就是减少bug量的原则,所以对UITableView进行了部分封装。并用 try catch进行异常捕获。由于水平有限,封装的不是很好。希望大家能够批评指正。具体代码如下
YWDataSource.h
//
// YWDataSource.h
// UITableView的封装
//
// Created by yuanwei on 15/4/20.
// Copyright (c) 2015年 YuanWei. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface YWDataSource : NSObject <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong ) NSMutableArray *tableData;
@property (nonatomic, assign ) BOOL isAllowEdit;
@property (nonatomic, copy ) NSString *sectionKey;
@property (nonatomic, copy ) NSString *rowKey;
@property (nonatomic, assign, readonly) CGFloat totalHeight;
@property (nonatomic, copy) UITableViewCell* (^cellForIndexPath)(NSIndexPath *indexPath);
//Number
@property (nonatomic, copy) NSInteger (^numberOfSectionsInTableView)(NSMutableArray *tableData);
@property (nonatomic, copy) NSInteger (^numberOfRowsInSection)(NSInteger section, id item);
@property (nonatomic, copy) NSArray *(^sectionIndexTitlesForTableView)(void);
//Header
@property (nonatomic, copy) CGFloat (^heightForHeaderInSection)(NSInteger section, id item);
@property (nonatomic, copy) UIView *(^viewForHeaderInSection)(NSInteger section, id item);
@property (nonatomic, copy) NSString *(^titleForHeaderInSection)(NSInteger section, id item);
//Footer
@property (nonatomic, copy) CGFloat (^heightForFooterInSection)(NSInteger section, id item);
@property (nonatomic, copy) UIView *(^viewForFooterInSection)(NSInteger section, id item);
@property (nonatomic, copy) NSString *(^titleForFooterInSection)(NSInteger section, id item);
//edit, delete
@property (nonatomic, copy) UITableViewCellEditingStyle (^editingStyleForRowAtIndexPath)(NSIndexPath *indexPath);
@property (nonatomic, copy) void (^deleteRowAtIndexPath)(NSIndexPath *indexPath, id item);
@property (nonatomic, copy) BOOL (^canEditRowAtIndexPath)(NSIndexPath *indexPath, id item);
//cellForRow, didSelect, height
@property (nonatomic, copy) void (^cellForRowAtCustom)(id cell, NSIndexPath *indexPath);
@property (nonatomic, copy) void (^cellForRowAtIndexPath)(id cell, NSIndexPath *indexPath, id item);
@property (nonatomic, copy) void (^didSelectRowAtCustom)(NSIndexPath *indexPath);
@property (nonatomic, copy) void (^didSelectRowAtIndexPath)(NSIndexPath *indexPath, id item);
@property (nonatomic, copy) void (^didDeselectRowAtIndexPath)(NSIndexPath *indexPath, id item);
@property (nonatomic, copy) CGFloat (^heightForRowAtIndexPath)(NSIndexPath *indexPath, id item);
- (instancetype)initWithTableData:(NSArray *)tableData
cellIdentifier:(NSString *)cellIdentifier
cellForRowAtIndexPath:(void (^)(id cell, NSIndexPath *indexPath, id item))cellForRowAtIndexPath;
- (id)itemAtSection:(NSInteger)section sectionKey:(NSString *)sectionKey;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath sectionKey:(NSString *)sectionKey rowKey:(NSString *)rowKey;
@end
YWDataSource.m
//
// YWDataSource.m
// UITableView的封装
//
// Created by yuanwei on 15/4/20.
// Copyright (c) 2015年 YuanWei. All rights reserved.
//
#import "YWDataSource.h"
@interface YWDataSource()
@property (nonatomic, copy ) NSString *cellIdentifier;
@end
@implementation YWDataSource
- (instancetype)initWithTableData:(NSArray *)tableData
cellIdentifier:(NSString *)cellIdentifier
cellForRowAtIndexPath:(void (^)(id cell, NSIndexPath *indexPath, id item))cellForRowAtIndexPath
{
self = [super init];
if (self) {
self.tableData = [NSMutableArray arrayWithArray:tableData];
self.cellIdentifier = cellIdentifier;
self.cellForRowAtIndexPath = cellForRowAtIndexPath;
self.isAllowEdit = NO;
_totalHeight = 0;
}
return self;
}
- (void)setTableData:(NSMutableArray *)tableData
{
_tableData = [NSMutableArray arrayWithArray:tableData];
}
#pragma mark -
#pragma mark - TableViewDataSource
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
_totalHeight = 0;
if (self.numberOfSectionsInTableView) {
return self.numberOfSectionsInTableView(self.tableData);
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
@try {
if (self.numberOfRowsInSection) {
return self.numberOfRowsInSection(section, [self itemAtSection:section sectionKey:self.sectionKey]);
} else if (self.numberOfSectionsInTableView) {
NSArray *sectionArr = [self itemAtSection:section sectionKey:self.sectionKey];
return [sectionArr count];
} else {
return [self.tableData count];
}
}
@catch (NSException *exception) {
NSLog(@"\n exception:%@", NSStringFromSelector(_cmd));
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
id cellItem;
@try {
if (!self.cellForIndexPath) {
cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (!cell) {
cell = self.cellForIndexPath(indexPath);
}
}
}
@catch (NSException *exception) {
NSLog(@"\n exception:%@", NSStringFromSelector(_cmd));
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:self.cellIdentifier];
}
if (self.cellForRowAtCustom) {
self.cellForRowAtCustom(cell, indexPath);
} else if (self.cellForRowAtIndexPath) {
cellItem = [self itemAtIndexPath:indexPath sectionKey:self.sectionKey rowKey:nil];
self.cellForRowAtIndexPath(cell, indexPath, cellItem);
}
return cell;
}
#pragma mark -
#pragma mark - TableViewDelegate
#pragma mark -
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
id item = [self itemAtIndexPath:indexPath sectionKey:self.sectionKey rowKey:nil];
[self.tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (self.deleteRowAtIndexPath) {
self.deleteRowAtIndexPath(indexPath, item);
}
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.canEditRowAtIndexPath) {
id item = [self itemAtIndexPath:indexPath sectionKey:self.sectionKey rowKey:nil];
return self.canEditRowAtIndexPath(indexPath, item);
}
return self.isAllowEdit;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (self.titleForHeaderInSection) {
return self.titleForHeaderInSection(section, [self.tableData objectAtIndex:section]);
} else {
return nil;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if (self.titleForFooterInSection) {
return self.titleForFooterInSection(section, [self.tableData objectAtIndex:section]);
} else {
return nil;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight = 0;
if (self.heightForRowAtIndexPath) {
id item = [self itemAtIndexPath:indexPath sectionKey:self.sectionKey rowKey:nil];
rowHeight = self.heightForRowAtIndexPath(indexPath, item);
} else {
rowHeight = tableView.rowHeight;
}
_totalHeight += rowHeight;
return rowHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
if (self.heightForHeaderInSection) {
return self.heightForHeaderInSection(section, [self.tableData objectAtIndex:section]);
} else {
return 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (self.heightForFooterInSection) {
return self.heightForFooterInSection(section, [self.tableData objectAtIndex:section]);
} else {
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (self.viewForHeaderInSection) {
return self.viewForHeaderInSection(section, [self.tableData objectAtIndex:section]);
} else {
return nil;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (self.viewForFooterInSection) {
return self.viewForFooterInSection(section, [self.tableData objectAtIndex:section]);
} else {
return nil;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
id cellItem;
if (!tableView.allowsMultipleSelection) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if (self.didSelectRowAtCustom) {
self.didSelectRowAtCustom(indexPath);
} else if (self.didSelectRowAtIndexPath) {
cellItem = [self itemAtIndexPath:indexPath sectionKey:self.sectionKey rowKey:self.rowKey];
self.didSelectRowAtIndexPath(indexPath, cellItem);
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.didDeselectRowAtIndexPath) {
id cellItem;
cellItem = [self itemAtIndexPath:indexPath sectionKey:self.sectionKey rowKey:self.rowKey];
self.didDeselectRowAtIndexPath(indexPath, cellItem);
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editingStyleForRowAtIndexPath) {
return self.editingStyleForRowAtIndexPath(indexPath);
} else if (self.isAllowEdit && tableView.allowsMultipleSelection) {
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (self.sectionIndexTitlesForTableView) {
return self.sectionIndexTitlesForTableView();
}
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
- (id)itemAtSection:(NSInteger)section sectionKey:(NSString *)sectionKey
{
id value;
@try {
if (self.numberOfSectionsInTableView) {
if (sectionKey) {
value = [self.tableData objectAtIndex:section][sectionKey];
} else {
value = [self.tableData objectAtIndex:section];
}
}
}
@catch (NSException *exception) {
value = nil;
NSLog(@"\n exception:%@", NSStringFromSelector(_cmd));
}
return value;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath sectionKey:(NSString *)sectionKey rowKey:(NSString *)rowKey
{
if (!self.tableData.count) {
return nil;
}
id value;
@try {
if (self.numberOfSectionsInTableView) {
if (sectionKey && rowKey) {
value = self.tableData[indexPath.section][sectionKey][indexPath.row][rowKey];
} else if (sectionKey) {
value = self.tableData[indexPath.section][sectionKey][indexPath.row];
} else if (rowKey) {
value = self.tableData[indexPath.section][indexPath.row][rowKey];
} else {
value = self.tableData[indexPath.section][indexPath.row];
}
} else if (rowKey) {
value = self.tableData[indexPath.row][rowKey];
} else {
value = self.tableData[indexPath.row];
}
}
@catch (NSException *exception) {
value = nil;
NSLog(@"\n exception:%@", NSStringFromSelector(_cmd));
}
return value;
}
@end
ViewController.m中的运用
//
// ViewController.m
// UITableView的封装
//
// Created by yuanwei on 15/4/20.
// Copyright (c) 2015年 YuanWei. All rights reserved.
//
#import "ViewController.h"
#import "YWDataSource.h"
#import "YwCell.h"
@interface ViewController ()
@property (nonatomic,weak) IBOutlet UITableView *tableView;
@property (nonatomic,strong) YWDataSource *tableViewDS;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"YwCell" bundle:nil] forCellReuseIdentifier:@"YwCell"];
_tableViewDS = [[YWDataSource alloc] initWithTableData:@[@1,@2,@3]
cellIdentifier:@"YwCell"
cellForRowAtIndexPath:^(YwCell *cell, NSIndexPath *indexPath, id item) {
[cell.iconImageView setImage:[UIImage imageNamed:@"share_wx_friends"]];
cell.contentLb.text = @"微信好友";
}];
[_tableViewDS setDidDeselectRowAtIndexPath:^(NSIndexPath *indexPath, id item) {
}];
[_tableView setDataSource:_tableViewDS];
[_tableView setDelegate:_tableViewDS];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
由于只是写了个一个小的Demo.UI方面和网络可自行添加修改
效果图如下
附上Demo下载地址:http://download.csdn.net/detail/u014466582/8613781