当前位置: 移动技术网 > IT编程>移动开发>IOS > ios对UITableView进行封装详情介绍

ios对UITableView进行封装详情介绍

2018年10月08日  | 移动技术网IT编程  | 我要评论

爱看网,秘闯金三角,当当商城

原由

从事ios工作有段时间了,其中uitableview频繁被使用中,这个过程中不断重复的创建加入代理,好麻烦,而且也让viewcontroller代码显的臃肿,因此做了下面的封装

思路

1.减少重复工作

tableview创建的工作做一次

2.类似的工作作一次

获取数据过程中就把最后需要多少个section,多少个cell的工作做完,后续直接用

3.是谁的东西就归谁做

tableviewcell的高度计算,数据填充都让cell自己去做

代码简单使用样例

简单的一个使用样例

1.viewcontroller

//数据源填充

-(void)createdatasource{

xctableviewdatasection *section = [[xctableviewdatasection alloc] init];

[section.rowmodelsarr addobject:@"1"];

[section.rowmodelsarr addobject:@"2"];

[section.rowmodelsarr addobject:@"3"];

[section.rowmodelsarr addobject:@"4"];

[self.datasource.sections addobject:section];

}

//cellforrow类似功能

-(class)xctableview:(uitableview *)tableview cellclassatmodel:(id)model{

return [tableviewcell1 class];

}

//tableviewcell的点击事件

-(void)xctableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath model:(id)model{

[self.tableview deselectrowatindexpath:indexpath animated:yes];

}

2.tableviewcell

设置高度

-(cgfloat)xctableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{

return 100;

}

赋值

-(void)xcsetmodel:(id)model forindexpath:(nsindexpath *)indexpath{

_lbname.text = model;

}

现有提供的代理

1.viewcontroller

/************设置tableviewcell *******/

//通过model判断加入不同的tableviewcell

-(class)xctableview:(uitableview *)tableview cellclassatmodel:(id)model;

//通过indexpath判断加入不同的tableviewcell

-(class)xctableview:(uitableview *)tableview cellclassatindexpath:(nsindexpath *)indexpath;

/********tableviewcell中操作反馈作用的代理 *******/

-(void)xctableviewcell:(xctableviewcell *)cell;

-(void)xctableviewcell:(xctableviewcell *)cell model:(id)model;

-(void)xctableviewcell:(xctableviewcell *)cell button:(uibutton *)button model:(id)model;

2. tableviewcell

/*****提供通过indexpath和数据两种方法判断设置高度 *******/

-(cgfloat)xctableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath;

-(cgfloat)xctableview:(uitableview *)tableview heightforrowatmodel:(id)model;

扩展的功能

1.提供self.datasource.isxib来设置支持使用xib和纯代码编写的两种cell

2.tableviewcell提供parameters可以传入请求数据,适用复杂业务需要外层json判断的

准备加入的(等我有空了就加)

1.tableviewcell中加入curcontroller,获取当前控制器(代码被我注释了,已经有了)

2.重写tableview和datasource方法

3.对header和footer的封装

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网