配色: 字号:
iOS开发中UITableview控件的基本使用及性能优化方法
2016-11-11 | 阅:  转:  |  分享 
  
iOS开发中UITableview控件的基本使用及性能优化方法

这篇文章主要介绍了iOS开发中UITableview控件的基本使用及性能优化方法,代码基于传统的Objective-C,需要的朋友可以参考下

UITableview控件基本使用

一、一个简单的英雄展示程序

NJHero.h文件代码(字典转模型)

复制代码代码如下:

#import

@interfaceNJHero:NSObject/头像/@property(nonatomic,copy)NSStringicon;/名称/@property(nonatomic,copy)NSStringname;/描述/@property(nonatomic,copy)NSStringintro;

-(instancetype)initWithDict:(NSDictionary)dict;+(instancetype)heroWithDict:(NSDictionary)dict;@end

NJViewController.m文件代码

#import"NJViewController.h"#import"NJHero.h"

@interfaceNJViewController()/保存所有的英雄数据/@property(nonatomic,strong)NSArrayheros;@property(weak,nonatomic)IBOutletUITableViewtableView;

@end



复制代码代码如下:

@implementationNJViewController

#pragmamark-懒加载-(NSArray)heros{if(_heros==nil){//1.获得全路径NSStringfullPath=[[NSBundlemainBundle]pathForResource:@"heros"ofType:@"plist"];//2.更具全路径加载数据NSArraydictArray=[NSArrayarrayWithContentsOfFile:fullPath];//3.字典转模型NSMutableArraymodels=[NSMutableArrayarrayWithCapacity:dictArray.count];for(NSDictionarydictindictArray){NJHerohero=[NJHeroheroWithDict:dict];[modelsaddObject:hero];}//4.赋值数据_heros=[modelscopy];}//4.返回数据return_heros;}

-(void)viewDidLoad{[superviewDidLoad];//设置Cell的高度//当每一行的cell高度一致的时候使用属性设置cell的高度self.tableView.rowHeight=60;self.tableView.delegate=self;}

#pragmamark-UITableViewDataSource//返回多少组-(NSInteger)numberOfSectionsInTableView:(UITableView)tableView{return1;}//返回每一组有多少行-(NSInteger)tableView:(UITableView)tableViewnumberOfRowsInSection:(NSInteger)section{returnself.heros.count;}//返回哪一组的哪一行显示什么内容-(UITableViewCell)tableView:(UITableView)tableViewcellForRowAtIndexPath:(NSIndexPath)indexPath{//1.创建CELLUITableViewCellcell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];//2.设置数据//2.1取出对应行的模型NJHerohero=self.heros[indexPath.row];//2.2赋值对应的数据cell.textLabel.text=hero.name;cell.detailTextLabel.text=hero.intro;cell.imageView.image=[UIImageimageNamed:hero.icon];//3.返回cellreturncell;}#pragmamark-UITableViewDelegate///当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度-(CGFloat)tableView:(UITableView)tableViewheightForRowAtIndexPath:(NSIndexPath)indexPath{if(1==indexPath.row){return180;}return44;}/

#pragmamark-控制状态栏是否显示/返回YES代表隐藏状态栏,NO相反/-(BOOL)prefersStatusBarHidden{returnYES;}@end

实现效果:



代码注意点:

(1)在字典转模型的代码处用下面的代码,为可变数组分配dictArray.count个存储空间,可以提高程序的性能

复制代码代码如下:

NSMutableArraymodels=[NSMutableArrayarrayWithCapacity:dictArray.count];

(2)设置cell的高度

有三种办法可以设置cell的高度

1)可以在初始加载方法中设置,self.tableView.rowHeight=60;这适用于当每一行的cell高度一致的时候,使用属性设置cell的高度。

2)在storyboard中设置,适用于高度一致

3)当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度

复制代码代码如下:

-(CGFloat)tableView:(UITableView)tableViewheightForRowAtIndexPath:(NSIndexPath)indexPath

{

if(1==indexPath.row){

return180;

}

return44;

}

二、cell的一些属性

代码示例:

复制代码代码如下:

#import"NJViewController.h"#import"NJHero.h"

@interfaceNJViewController()/保存所有的英雄数据/@property(nonatomic,strong)NSArrayheros;@property(weak,nonatomic)IBOutletUITableViewtableView;

@end



复制代码代码如下:

@implementationNJViewController

#pragmamark-懒加载-(NSArray)heros{if(_heros==nil){//1.获得全路径NSStringfullPath=[[NSBundlemainBundle]pathForResource:@"heros"ofType:@"plist"];//2.更具全路径加载数据NSArraydictArray=[NSArrayarrayWithContentsOfFile:fullPath];//3.字典转模型NSMutableArraymodels=[NSMutableArrayarrayWithCapacity:dictArray.count];for(NSDictionarydictindictArray){NJHerohero=[NJHeroheroWithDict:dict];[modelsaddObject:hero];}//4.赋值数据_heros=[modelscopy];}//4.返回数据return_heros;}

-(void)viewDidLoad{[superviewDidLoad];//设置Cell的高度//当每一行的cell高度一致的时候使用属性设置cell的高度self.tableView.rowHeight=60;self.tableView.delegate=self;

}

#pragmamark-UITableViewDataSource//返回多少组-(NSInteger)numberOfSectionsInTableView:(UITableView)tableView{return1;}//返回每一组有多少行-(NSInteger)tableView:(UITableView)tableViewnumberOfRowsInSection:(NSInteger)section{returnself.heros.count;}//返回哪一组的哪一行显示什么内容-(UITableViewCell)tableView:(UITableView)tableViewcellForRowAtIndexPath:(NSIndexPath)indexPath{//1.创建CELLUITableViewCellcell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];//2.设置数据//2.1取出对应行的模型NJHerohero=self.heros[indexPath.row];//2.2赋值对应的数据cell.textLabel.text=hero.name;cell.detailTextLabel.text=hero.intro;cell.imageView.image=[UIImageimageNamed:hero.icon];//2.3设置cell的辅助视图//cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;if(0==indexPath.row){cell.accessoryView=[UIButtonbuttonWithType:UIButtonTypeContactAdd];}else{cell.accessoryView=[[UISwitchalloc]init];}//UIButtonbtn=[[UIButtonalloc]init];//btn.backgroundColor=[UIColorredColor];//cell.accessoryView=btn;//2.4设置cell的背景颜色cell.backgroundColor=[UIColorblueColor];//设置默认状态的背景//UIViewview=[[UIViewalloc]init];//view.backgroundColor=[UIColorblueColor];//cell.backgroundView=view;UIImageViewiv=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"buttondelete"]];cell.backgroundView=iv;//设置选中状态的背景UIViewview2=[[UIViewalloc]init];view2.backgroundColor=[UIColorpurpleColor];cell.selectedBackgroundView=view2;//3.返回cellreturncell;}

#pragmamark-控制状态栏是否显示/返回YES代表隐藏状态栏,NO相反/-(BOOL)prefersStatusBarHidden{returnYES;}@end

实现效果:



cell的一些属性:

(1)设置cell的辅助视图,设置cell.accessoryView(系统提供了枚举型,也可以自定义@父类指针指向子类对象);

(2)设置cell的背景颜色,有两种方式可以设置cell的背景颜色:

通过backgroundColor和backgroundView都可以设置cell的背景。但是backgroundView的优先级比backgroundColor的高,所以如果同时设置了backgroundColor和backgroundView,那么backgroundView会盖住backgroundColor

示例:cell.backgroundColor=[UIColorblueColor];

(3)设置cell默认状态的背景

示例1:

复制代码代码如下:

UIViewview=[[UIViewalloc]init];

view.backgroundColor=[UIColorblueColor];

cell.backgroundView=view;

示例2:

复制代码代码如下:

UIImageViewiv=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"buttondelete"]];

cell.backgroundView=iv;(父类指针指向子类对象,可以使用图片用简单的操作设置绚丽的效果)

(4)设置cell选中状态的背景

示例:

复制代码代码如下:

UIViewview2=[[UIViewalloc]init];

view2.backgroundColor=[UIColorpurpleColor];

cell.selectedBackgroundView=view2;

三、tableview的一些属性

代码示例:

复制代码代码如下:

#import"NJViewController.h"

@interfaceNJViewController()

@end

@implementationNJViewController

-(void)viewDidLoad{[superviewDidLoad];

//1.创建tableviewUITableViewtableview=[[UITableViewalloc]init];tableview.frame=self.view.bounds;

//2.设置数据源tableview.dataSource=self;

//3.添加tableview到view[self.viewaddSubview:tableview];//4.设置分割线样式//tableview.separatorStyle=UITableViewCellSeparatorStyleNone;

//5.设置分割线颜色接收的参数是颜色的比例值tableview.separatorColor=[UIColorcolorWithRed:0/255.0green:255/255.0blue:0/255.0alpha:255/255.0];//设置tableview的头部视图tableview.tableHeaderView=[UIButtonbuttonWithType:UIButtonTypeContactAdd];tableview.tableFooterView=[[UISwitchalloc]init];}

-(NSInteger)numberOfSectionsInTableView:(UITableView)tableView{return1;}-(NSInteger)tableView:(UITableView)tableViewnumberOfRowsInSection:(NSInteger)section{return10;}

-(UITableViewCell)tableView:(UITableView)tableViewcellForRowAtIndexPath:(NSIndexPath)indexPath{//1.创建cellUITableViewCellcell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];

//2.设置cell的数据cell.textLabel.text=[NSStringstringWithFormat:@"%d",indexPath.row];

//3.返回cellreturncell;}

-(BOOL)prefersStatusBarHidden{returnYES;}@end

实现效果:



tableview的一些属性:

(1)设置分割样式(tableview.separatorStyle),这是个枚举类型

(2)设置分割线的颜色,可以直接使用系统给出的颜色,如果系统给定的颜色不能满足需求时,也可以自定义。

补充:颜色分为24位和32位的,如下

24bit颜色

R8bit0~255

G8bit0~255

B8bit0~255

32bit颜色

A8bit0~255(tou)

R8bit

G8bit

B8bit

#ffffff白色

#000000黑色

#ff0000红色

#2550000

设置为自定义颜色的实例:

复制代码代码如下:

tableview.separatorColor=[UIColorcolorWithRed:0/255.0green:255www.sm136.com0blue:0/255.0alpha:255/255.0];

//接收的参数是颜色的比例值

(3)设置顶部和底部视图

复制代码代码如下:

tableview.tableHeaderView//顶部

tableview.tableFooterView//底部

UITableviewcell的性能问题一、UITableviewcell的一些介绍

UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每?行

UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图

辅助指示视图的作?是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显?示辅助指?示视图),其他值如下:

复制代码代码如下:

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

还可以通过cell的accessoryView属性来自定义辅助指示视图(?如往右边放一个开关)

二、问题

cell的工作:在程序执行的时候,能看到多少条,它就创建多少条数据,如果视图滚动那么再创建新显示的内容。(系统自动调用)。即当一个cell出现在视野范围内的时候,就会调用创建一个cell。这样的逻辑看上去没有什么问题,但是真的没有任何问题吗?

当创建调用的时候,我们使用nslog打印消息,并打印创建的cell的地址。我们发现如果数据量非常大,用户在短时间内来回滚动的话,那么会创建大量的cell,一直开辟空间,且如果是往回滚,通过打印地址,我们会发现它并没有重用之前已经创建的cell,而是重新创建,开辟新的存储空间。

那有没有什么好的解决办法呢?

三、cell的重用原理

(1)iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象

(2)重?原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource则会用新的数据来配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象。这样可以让创建的cell的数量维持在很低的水平,如果一个窗口中只能显示5个cell,那么cell重用之后,只需要创建6个cell就够了。

(3)注意点:还有?个非常重要的问题:有时候需要自定义UITableViewCell(用?个子类继承UITableViewCell),而且每?行?的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重?用UITableViewCell时可能会得到错误类型的UITableViewCell

解决?方案:UITableViewCell有个NSStringreuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化?一个UITableViewCell对象。

图片示例:



说明:一个窗口放得下(可视)三个cell,整个程序只需要创建4个该类型的cell即可。

四、cell的优化代码

代码示例:

复制代码代码如下:

#import"NJViewController.h"#import"NJHero.h"

//#defineID@"ABC"

@interfaceNJViewController(www.visa158.com)/保存所有的英雄数据/@property(nonatomic,strong)NSArrayheros;@property(weak,nonatomic)IBOutletUITableViewtableView;

@end



复制代码代码如下:

@implementationNJViewController

#pragmamark-懒加载-(NSArray)heros{if(_heros==nil){//1.获得全路径NSStringfullPath=[[NSBundlemainBundle]pathForResource:@"heros"ofType:@"plist"];//2.更具全路径加载数据NSArraydictArray=[NSArrayarrayWithContentsOfFile:fullPath];//3.字典转模型NSMutableArraymodels=[NSMutableArrayarrayWithCapacity:dictArray.count];for(NSDictionarydictindictArray){NJHerohero=[NJHeroheroWithDict:dict];[modelsaddObject:hero];}//4.赋值数据_heros=[modelscopy];}//4.返回数据return_heros;}

-(void)viewDidLoad{[superviewDidLoad];//设置Cell的高度//当每一行的cell高度一致的时候使用属性设置cell的高度self.tableView.rowHeight=160;}

#pragmamark-UITableViewDataSource//返回多少组-(NSInteger)numberOfSectionsInTableView:(UITableView)tableView{return1;}//返回每一组有多少行-(NSInteger)tableView:(UITableView)tableViewnumberOfRowsInSection:(NSInteger)section{returnself.heros.count;}//当一个cell出现视野范围内的时候就会调用//返回哪一组的哪一行显示什么内容-(UITableViewCell)tableView:(UITableView)tableViewcellForRowAtIndexPath:(NSIndexPath)indexPath{//定义变量保存重用标记的值staticNSStringidentifier=@"hero";//1.先去缓存池中查找是否有满足条件的CellUITableViewCellcell=[tableViewdequeueReusableCellWithIdentifier:identifier];//2.如果缓存池中没有符合条件的cell,就自己创建一个Cellif(cell==nil){//3.创建Cell,并且设置一个唯一的标记cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];NSLog(@"创建一个新的Cell");}//4.给cell设置数据NJHerohero=self.heros[indexPath.row];cell.textLabel.text=hero.name;cell.detailTextLabel.text=hero.intro;cell.imageView.image=[UIImageimageNamed:hero.icon];//NSLog(@"%@-%d-%p",hero.name,indexPath.www.hunanwang.netrow,cell);//3.返回cellreturncell;}

#pragmamark-控制状态栏是否显示/返回YES代表隐藏状态栏,NO相反/-(BOOL)prefersStatusBarHidden{returnYES;}@end

缓存优化的思路:

(1)先去缓存池中查找是否有满足条件的cell,若有那就直接拿来

(2)若没有,就自己创建一个cell

(3)创建cell,并且设置一个唯一的标记(把属于“”的给盖个章)

(4)给cell设置数据

注意点:

定义变量用来保存重用标记的值,这里不推荐使用宏(#define来处理),因为该变量只在这个作用域的内部使用,且如果使用宏定义的话,定义和使用位置太分散,不利于阅读程序。由于其值不变,没有必要每次都开辟一次,所以用static定义为一个静态变量。























献花(0)
+1
(本文系白狐一梦首藏)