- 浏览: 85087 次
- 性别:
- 来自: 成都
最新评论
.h
//
// PublicTableView.h
// JointCrm
//
// Created by Mac on 15/10/27.
// Copyright © 2015年 Mac. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^TableBlock)(UITableView *tableView,NSIndexPath *indexPath);
@interface PublicTableView : UITableView
/**
* 定义一个公共的TableView,然后可以在使用tableview的地方使用
*
* @param frame UITableView的frame
* @param style UITableViewStyle
* @param className cell Class
* @param mcellMethod cell调用的方法
* @param isUserNib 是否使用nib
*
* @return cell
*/
-(instancetype)initWithFrame:(CGRect)frame
style:(UITableViewStyle)style
cellWithClassName:(NSString *)className
cellWithMethod:(NSString *)cellMethod
cellWithIsUserNib:(BOOL)isUserNib;
// 数据列表
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, copy) TableBlock tableBlock;
// 数据行选择时所触发的block
- (void)blockWithCellSelectRowAtIndexPath:(TableBlock)tableBlock;
@end
.m
//
// PublicTableView.m
// JointCrm
//
// Created by Mac on 15/10/27.
// Copyright © 2015年 Mac. All rights reserved.
//
#import "PublicTableView.h"
#import <objc/objc-runtime.h>
@interface PublicTableView()<UITableViewDataSource,UITableViewDelegate>{
NSString *cellClassName;
NSString *cellInvokMethod;
}
@end
@implementation PublicTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style cellWithClassName:(NSString *)className cellWithMethod:(NSString *)cellMethod cellWithIsUserNib:(BOOL)isUserNib{
if (self = [super initWithFrame:frame style:style]) {
self.delegate = self;
self.dataSource = self;
cellClassName = className;
cellInvokMethod = cellMethod;
if (isUserNib) {
[self registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];
}else{
[self registerClass:NSClassFromString(className) forCellReuseIdentifier:className];
}
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellClassName forIndexPath:indexPath];
if ([cell respondsToSelector:NSSelectorFromString(cellInvokMethod)]) {
id value = self.dataList[indexPath.row];
[self sendMsg:cell method:NSSelectorFromString(cellInvokMethod) value1:value value2:indexPath];
}else{
DLog(@"%@",@"cell未实现该方法");
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.tableBlock(tableView,indexPath);
}
-(void)blockWithCellSelectRowAtIndexPath:(TableBlock)tableBlock{
self.tableBlock = tableBlock;
}
- (void)sendMsg:(id)rever method:(SEL)method value1:(id)value1 value2:(id)value2{
int (*action)(id, SEL, id, id) = (int (*)(id, SEL, id, id)) objc_msgSend;
action(rever, method, value1, value2);
}
@end
使用:
PublicTableView *publicTableView = [[PublicTableView alloc] initWithFrame:self.view.frame
style:UITableViewStylePlain
cellWithClassName:@"MyCell"
cellWithMethod:@"cellConfig:"
cellWithIsUserNib:YES];
[self.view addSubview:publicTableView];
publicTableView.dataList = dataList;
[publicTableView blockWithCellSelectRowAtIndexPath:^(UITableView *tableView, NSIndexPath *indexPath) {
}];
注意:这里只需要把每个cell的类名,及配置数据的方法传进去就可以了
//
// PublicTableView.h
// JointCrm
//
// Created by Mac on 15/10/27.
// Copyright © 2015年 Mac. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^TableBlock)(UITableView *tableView,NSIndexPath *indexPath);
@interface PublicTableView : UITableView
/**
* 定义一个公共的TableView,然后可以在使用tableview的地方使用
*
* @param frame UITableView的frame
* @param style UITableViewStyle
* @param className cell Class
* @param mcellMethod cell调用的方法
* @param isUserNib 是否使用nib
*
* @return cell
*/
-(instancetype)initWithFrame:(CGRect)frame
style:(UITableViewStyle)style
cellWithClassName:(NSString *)className
cellWithMethod:(NSString *)cellMethod
cellWithIsUserNib:(BOOL)isUserNib;
// 数据列表
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, copy) TableBlock tableBlock;
// 数据行选择时所触发的block
- (void)blockWithCellSelectRowAtIndexPath:(TableBlock)tableBlock;
@end
.m
//
// PublicTableView.m
// JointCrm
//
// Created by Mac on 15/10/27.
// Copyright © 2015年 Mac. All rights reserved.
//
#import "PublicTableView.h"
#import <objc/objc-runtime.h>
@interface PublicTableView()<UITableViewDataSource,UITableViewDelegate>{
NSString *cellClassName;
NSString *cellInvokMethod;
}
@end
@implementation PublicTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style cellWithClassName:(NSString *)className cellWithMethod:(NSString *)cellMethod cellWithIsUserNib:(BOOL)isUserNib{
if (self = [super initWithFrame:frame style:style]) {
self.delegate = self;
self.dataSource = self;
cellClassName = className;
cellInvokMethod = cellMethod;
if (isUserNib) {
[self registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];
}else{
[self registerClass:NSClassFromString(className) forCellReuseIdentifier:className];
}
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellClassName forIndexPath:indexPath];
if ([cell respondsToSelector:NSSelectorFromString(cellInvokMethod)]) {
id value = self.dataList[indexPath.row];
[self sendMsg:cell method:NSSelectorFromString(cellInvokMethod) value1:value value2:indexPath];
}else{
DLog(@"%@",@"cell未实现该方法");
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.tableBlock(tableView,indexPath);
}
-(void)blockWithCellSelectRowAtIndexPath:(TableBlock)tableBlock{
self.tableBlock = tableBlock;
}
- (void)sendMsg:(id)rever method:(SEL)method value1:(id)value1 value2:(id)value2{
int (*action)(id, SEL, id, id) = (int (*)(id, SEL, id, id)) objc_msgSend;
action(rever, method, value1, value2);
}
@end
使用:
PublicTableView *publicTableView = [[PublicTableView alloc] initWithFrame:self.view.frame
style:UITableViewStylePlain
cellWithClassName:@"MyCell"
cellWithMethod:@"cellConfig:"
cellWithIsUserNib:YES];
[self.view addSubview:publicTableView];
publicTableView.dataList = dataList;
[publicTableView blockWithCellSelectRowAtIndexPath:^(UITableView *tableView, NSIndexPath *indexPath) {
}];
注意:这里只需要把每个cell的类名,及配置数据的方法传进去就可以了
发表评论
-
block语法
2015-12-11 10:34 486How Do I Declare A Block in Obj ... -
禁止WebView长按事件
2015-11-04 16:05 1146在webViewDidFinishLoad调用: - (voi ... -
记录一些不错的文章
2015-10-09 20:04 576好久没有写ios了,这篇文章主要是记录一些看到的不错的文章: ... -
封装录音View
2015-06-26 16:13 570使用方法: 直接把XHRecrodView添加到control ... -
AFNetWorking请求WebService
2015-06-15 17:22 793.h #import <Foundation/Foun ... -
压缩图片,如果图片大于100kb,就循环压缩
2015-06-02 10:37 2241// 压缩图片,如果图片大于100kb,就循环压缩 + (NS ... -
weakSelf
2015-05-11 14:44 534快速的定义一个weakSelf 当然是用于block里面啦 ... -
UINavigationItem 位置问题
2015-05-06 14:09 992解决ios7 UINavigationItem 位置偏移问题 ... -
ios Icon及启动图集合
2015-05-04 09:13 620做icon和启动图按这个尺寸来 -
把图片压缩到指定大小(kb)
2015-01-19 16:32 4979UIImage *image=[UIImage imageNa ... -
获取当前时间属于该月的第几周
2015-01-06 15:04 1076+(NSInteger) indexWeekOfDateInM ... -
iOS开发的一些奇巧淫技
2014-12-31 11:13 734iOS开发的一些奇巧淫技 http://www.coco ... -
iOS中使用block进行网络请求回调
2014-06-23 16:26 5501转自: http://www.tuicool.com/arti ... -
ios程序异常crash捕获与拦截
2014-06-06 22:09 528转:http://www.sharejs.com/codes/ ... -
设置TabBar选中与未选中图片
2014-04-29 18:07 655-(void)settingTabbarController{ ... -
自定义的NavigationBar,我觉得还不错
2014-04-28 18:03 556地址1:http://code.cocoachina.com ... -
UITableView点击展开cell
2014-04-25 15:14 103531.定义控制cell的两个变量 //最近打开的ind ... -
分享一个非常好的东西
2014-04-09 17:44 683http://makeappicon.com/ 传一个10 ... -
IOS 基于APNS消息推送原理与实现(JAVA后台)
2014-04-09 17:30 902转:http://cshbbrain.iteye.com/bl ... -
ios开发申请发布证书和发布应用到app store
2014-03-21 11:03 7071.http://www.360doc.com/content ...
相关推荐
tableview代理数据源每次写tableview不用重复的粘贴复制数据源和代理方法。代码不适合用多变的cell,
只需一行代码,通过链式风格快速为UITableView创建Delegate和DataSource,满足你对UITableView的一切幻想
LightTableView轻量级tableview 分离delegate datasource cell
请不要直接下载代码(案例代码超过5M) 完整代码 Github:https://github.com/Jeykit/MUKit 详细介绍: ... 优点: 只需要一个block就可以完成UITableView显示,无需写烦人的delegate和DataSource
_tableView.delegate = self; _tableView.dataSource = self; _tableView.backgroundColor = [UIColor lightGrayColor]; //内容由kImageOriginHight 处开始显示。 _tableView.contentInset=UIEdgeInsetsMake...
SXTableViewManager分离出datasource和delegate和其他操作,更加方便的管理tableVIew
只需一行代码,通过链式风格快速配置UITableView包括创建Delegate和DataSource等各种功能,满足你对UITableView的一切幻想,https://github.com/hongdong/HDTableViewMaker
在每个controller中都需要设置dataSource和delegate的烦恼,并UITableViewCell与DataSource tableView解耦合 具体表现为 Adapter与UITableView、Adapter与UITableViewCell完全解耦合。 只需要在controller中设置好...
WBTableView tableview 只有一行,多列允许水平滚动#用法1、导入头文件 #import "WBTableView.h"2、创建WBTableView实例 @implementation ViewController{ WBTableView * _tableView;}......_tableView = [...
宝宝们再也不用担心一不小心忘记设置(tableView.dataSource = self和tableView.delegate = self)或者忘记遵循协议带来的种种烦恼! 避免使用UITableViewDataSource和UITabBarDelegate的API带来的代码分块不方便集中...
刷新由设计的一个简单的iOS实现。 受到启发。 这只是一个粗略的实现,只是为了好玩。 有许多功能需要实现。截屏特征分开的飞行旅程分为两个部分,取决于网络有一个默认的加载动画(只需更改天空的颜色) 刷新完成后...
基于数据驱动页面的理念,接管了UITableView的delegate和dataSource的逻辑,开发者只需要关心数据的处理,避免了冗长的判断,让代码更加易于维护。 比如一个页面里面是UITableView,有5种不同的Cell。按照传统的...
在每个controller中都需要设置dataSource和delegate的烦恼,并UITableViewCell与DataSource tableView解耦合 具体表现为 Adapter与UITableView、Adapter与UITableViewCell完全解耦合。 只需要在controller中设置好...
使用MUScrollManage解决UIScrollView嵌套滚动只需两步,而且不会影响已经设置的delegate和dataSource 1.定义一个MUScrollManager属性,如下 @property (nonatomic,strong) MUScrollManager *manager; 2.初始化一...
在iOS开发的过程中,UITableview是使用频率很高的控件之一,今天写的优化方法不是关于性能优化方面的,主要从为Controller瘦身方面...在使用tableView的时候不可避免的要谈到tableView的delegate和dataSource两个代理.
在使用tableView的时候不可避免的要谈到tableView的delegate和dataSource两个代理,我们经常会把这两个代理赋给Controller,在Controller里面我们会实现它的几个代理方法,最常见的有以下几个:
//此方法可以有参数,也可以没有参数,如果没有参数系统不会给你穿参数,如果有参数,只能有一个参数,无论你所指定的参数类型是什么,系统只会把tf本身给传过去 [tf addTarget:self action:@selector(down:) ...
XCMultiSortTableView 目的 XCMultiSortTableView是固定头和可排序的列tableView。 请访问演示演示,核心部分是演示文件中的XCMultiSortTableView文件夹。...tableView.datasource = self; [self.view