UITableView

UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped

里的方法:

tableView处理步骤

#pragma mark 1.有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

#pragma mark 2.第section组头部控件有多高

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

#pragma mark 3.第section组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

#pragma mark 4.indexPath这行的cell有多高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 5.indexPath这行的cell长什么样子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 6.第section组头部显示什么控件

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

//每当有一个cell进入视野屏幕就会调用,所以在这个方法内部就需要优化。

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

if(cell==nil){

//在这里面做创建的工作。循环优化。防止刷新cell进入屏幕的时候重复的创建

}

}

//当调用reloadData的时候,会重新刷新调用数据源内所有方法,其他事情都不会做呀

[selfreloadData]

//这个方法只有在一开始有多少条数据才会算多少个高度,这个方法只会调用一次,但是每次reloadData的时候也会调用

//而且会一次性算出所有cell的高度,比如有100条数据,一次性调用100次

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView//右侧索引

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath//行点击事件

NSIndexPath*path = [self.tableViewindexPathForSelectedRow];//获得被选中的indexPath可以得到section,row

[self.tableViewreloadRowsAtIndexPaths:[self.tableViewindexPathsForSelectedRows]withRowAnimation:UITableViewRowAnimationNone];//刷新table指定行的数据

[self.tableViewreloadData];//刷新table所有行的数据

UITableView常用属性:

UITableView*tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,460)style:UITableViewStylePlain];//初始化表格

分隔线属性

tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;//UITableViewCellSeparatorStyleNone;

[self.tableViewsetSeparatorStyle:UITableViewCellSeparatorStyleNone];//取消分隔线

tableView.separatorColor= [UIColorlightGrayColor];

条目多选

tableView.allowsMultipleSelection=YES;

//设置标题行高

[_tableViewsetSectionHeaderHeight:kHeaderHeight];

[_tableViewsetSectionFooterHeight:0];

//设置表格行高

[_tableViewsetRowHeight:50];

//设置背景色

self.tableView.backgroundView优先级高,如果要设置backgroundColor的时候要先把view设置为nil

self.tableView.backgroundColor

//在tableView的头部或者尾部添加view,footerView宽度是不用设置的

xxxView.bounds = CGRectMake(0,0,0,height);

self.tableView.tableFooterView=xxxView;

self.tableView.tableHeaderView=xxxView;

UIButton*bt = (UIButton*)[self.contentViewviewWithTag:i+100];

增加tableview滚动区域

self.tableView.contentInset=UIEdgeInsetsMake(0,0, xx,0);


UITableViewCell

//创建UITableViewCell

UITableViewCell*cell = [[UITableViewCellalloc]

initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

[cell.textLabelsetBackgroundColor:[UIColorclearColor]];//清空标签背景颜色

cell.backgroundView=xx;//设置背景图片

cell.backgroundVColor=xx;

cell.selectedBackgroundView= selectedBgView; //设置选中时的背景颜色

cell.accessoryView = xxxView;//设置右边视图

[cellsetAccessoryType:UITableViewCellAccessoryNone]; //设置右侧箭头

[selfsetSelectionStyle:UITableViewCellSelectionStyleNone];//选中样式

cell.selectionStyle=UITableViewCellSelectionStyleBlue;

//设置cell的高度

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

contentView下默认有3个子视图,其中的2个是UILabel,通过textLabel和detailTextLabel属性访问,第3个是UIImageView,通过imageView属性访问.

UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

#pragma mark -重新调整UITalbleViewCell中的控件布局

- (void)layoutSubviews{

[superlayoutSubviews];

}

cell里面还有一个contentView


UITableViewCell表格优化

UITableViewCell对象的重用原理:

重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象

还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,时可能会得到错误类型的UITableViewCell那么UITableView在重用UITableViewCell。解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

/**

单元格优化

1.标示符统一,使用static的目的可以保证表格标示符永远只有一个

2.首先在缓冲池中找名为"myCell"的单元格对象

3.如果没有找到,实例化一个新的cell

**/

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

staticNSString*cellIdentifier =@"myCell";

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];

//使用这种方法不用判断下面的cell

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifierforIndexPath:indexPath];

if(cell ==nil) {

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];

}

returncell;

}


表格的编辑模式

删除、插入

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;开启表格编辑状态

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath{

返回表格编辑编辑样式。不实现默认都是删除

return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert

}

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

//根据editingStyle处理是删除还是添加操作

完成删除、插入操作刷新表格

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

}

移动

- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath

sourceIndexPath 移动的行

destinationIndexPath 目标的行


自定义表格行UITableViewCell

storyboard方式创建:

直接拖到UITableView里面设置UITableViewCell

注意:

1.通过XIB或者Storyboard自定义单元格时,在xib和Storyboard里面需要指定单元格的可重用标示符Identifier

2.注意表格的优化中的差别

在Storyboard中两者等效

xxCell*cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

xxCell*cell1 = [tableViewdequeueReusableCellWithIdentifier:CellIdentifierforIndexPath:indexPath];

在xib文件中有差别:

第一种情况,只能在iOS 6以上使用,如果在viewDidLoad注册了nib文件,并且指定了“单元格”的可重用标示符,那么

dequeueReusableCellWithIdentifier:

dequeueReusableCellWithIdentifier:forIndexPath:

方法是等效的。如果在viewDidLoad中注册了nib文件,表格缓冲池中的管理,有系统接管!

第二种情况,是在iOS 4以上均可以使用,如果没有在viewDidLoad注册nib文件,那么,只能使用

dequeueReusableCellWithIdentifier:并且需要判断cell没有被实例化,并做相应的处理

在代码创建中差别:

用代码创建cell中的处理和nib一样,注册了cell就有系统接管并且可以用带forIndexPath的方法,没有注册就要自己去实例化cell,不能用带forIndexPath的方法

[tableViewregisterClass:XxxCellclass]forCellReuseIdentifier:@"xxCell"];

xib方式创建:

//注册Identifier

- (void)viewDidLoad{

[superviewDidLoad];

/**

注意:以下几句注册XIB的代码,一定要在viewDidLoad中!

注册XIB文件,获得根视图,并且转换成TableView,为tableView注册xib

Identifier名要在xib文件中定义,并且保持一致

**/

UINib*nib = [UINibnibWithNibName:@"BookCell"bundle:[NSBundlemainBundle]];

UITableView*tableView = (UITableView*)self.view;

[tableViewregisterNib:nibforCellReuseIdentifier:@"bookCell"];

}

//没有注册Identifier只能使用下面方法

staticNSString*CellIdentifier =@"bookCell";

BookCell*cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

if(cell ==nil) {

cell = [[BookCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

NSBundle*bundle = [NSBundlemainBundle];

NSArray*array = [bundleloadNibNamed:@"BookCell"owner:niloptions:nil];

cell = [arraylastObject];

}

代码方式创建:

建立UITableViewCell的类,继承UITableViewCell

往cell里面加入view的时候注意点:

//新建的组件放入contentView中

[self.contentViewaddSubview:xxView];

//设置图片拉伸属性stretch

UIImage*normalImage = [UIImage imageNamed:@"xx.png"];

normalImage = [normalImagestretchableImageWithLeftCapWidth:

normalImage.size.width/2topCapHeight:normalImage.size.height/2];

//在tableView里面viewDiDLoad里面要注册cell类

[tableViewregisterClass:XxxCellclass]forCellReuseIdentifier:@"xxCell"];


自定义表格中Header

//自定义表格在这个方法中定义

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section

-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容