工做了将近两个月,共接手两个项目,在项目中用的最多的就是UITableView了,可是也是问题出现的最多的地方,因为一开始不熟练,致使不少问题花了很长时间才解决。因此利用这两天空闲时间,好好梳理一下这个控件,但愿可以方便之后的开发工做。node
个人介绍是从我建立UITableView的顺序提及。设计模式
1、概述:框架
UITableView继承与UIScrollView。这两个控件的异同点以下:函数
相同点:UITableView实现了UIScrollView协议,且继承于UIScrollView,两个组件均可以滑动。 spa
相异点:UITableView是逐步加载UITableViewCell,之后每次滚动时,重绘cell;UIScrollView是初始化全部其子视图,之后每次滚动,再也不重绘其子视图,因此UITableView滑动起来比较卡,而UIScrollView滑动比较顺畅;在数据量比较多的状况下,应该使用UITableView,由于cell能够复用,使用内存相对较少,而UIScrollView使用内存较多;另外UITableView能够刷新数据,reloaddata等,而scrollview的数据是固定的,除非从新初始化。.net
2、建立UITableView设计
我正常建立UITableView,会把它封装到一个方法里面。这样清楚一点。 代理
-(void)initTableView { helpTableView = [[UITableView alloc]initWithFrame:CGRectMake(5, navheight + 7.5, 305, 480) style:UITableViewStyleGrouped]; helpTableView.delegate = self; helpTableView.dataSource = self; helpTableView.backgroundView = nil; [helpTableView setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]]; helpTableView.separatorColor = [UIColor clearColor]; helpTableView.separatorStyle = UITableViewCellSeparatorStyleNone; helpTableView.scrollEnabled = NO; [self.view addSubview:helpTableView];
[helpTableView release];
}
一、首先介绍第一句代码,UITableView的style。code
UITableViewStyleGrouped和UITableViewStylePlain的区别我认为就是UITableViewStyleGrouped背后贴了一层背景图。可是咱们能够经过blog
helpTableView.backgroundView = nil; [helpTableView setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];
这段代码来去除背景。而后绘制上你须要的背景色。helpTableView.backgroundView = nil;这句代码必定要有。
二、dataSource和delegate
在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承UITableViewController实现的方法。
dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操做类型(insert,delete和 reordering),并根据用户的操做进行相应的数据更新操做,若是数据没有根据操做进行正确的更新,可能会致使显示异常,甚至crush。
delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。
dataSource和delegate他们实际上是Cocoa框架的一种设计模式,叫策略模式,改天能够另外开一篇博文介绍专门介绍策略模式。
三、UITableView的一些属性
关于属性和一些方法能够参考这篇文章,讲的比较详细。
3、实现代理必须实现的三个方法。
一、UITableView有三个必须实现的核心方法,分别以下:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
这个方法能够分段显示或者单个列表显示咱们的数据。以下,左边为分段显示,右边为单个列表显示:
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
这个方法返回每一个分段的行数,不一样分段返回不一样的行数能够用switch来作,若是是单个列表就直接返回单个你想要的函数便可。
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法是返回咱们调用的每个单元格。经过咱们索引的路径的section和row来肯定。
二、UITableView的委托方法
使用委托是为了响应用户的交互动做,好比下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供咱们选择。
//设置Section的数量 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return TitleData; } //设置每一个section显示的Title - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{ return @"Andy-清风"; } //指定有多少个分区(Section),默认为1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } //指定每一个分区中有多少行,默认为1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ } //设置每行调用的cell -(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier: SimpleTableIdentifier]; if (cell == nil) { cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; } cell.imageView.image=image;//未选cell时的图片 cell.imageView.highlightedImage=highlightImage;//选中cell后的图片 cell.text=@”Andy-清风”; return cell; } //设置让UITableView行缩进 -(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; return row; } //设置cell每行间隔的高度 - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 40; } //返回当前所选cell NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section]; [TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone]; //设置UITableView的style [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone]; //设置选中Cell的响应事件 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失 } //设置选中的行所执行的动做 -(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; return indexPath; } //设置划动cell是否出现del按钮,可供删除数据里进行处理 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath { } //设置删除时编辑状态 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } //右侧添加一个索引表 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ }
参考连接: