IOS开发中UITableView(表视图)的滚动优化及自定义Cell

时间:2023-03-08 20:44:23
IOS开发中UITableView(表视图)的滚动优化及自定义Cell

IOS开发中UITableView(表视图)的滚动优化及自定义Cell

  IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITableView实现的。UITableView这个控件中的列表的每一行是一个cell,当UITableView中cell数量特别大的时候,由于每次都需要alloc分配内存并初始化,会导致app运行不流畅,所以可以使用苹果提供的几个方法进行优化,我把这个过程记录下来供自己以后查阅。

  当然,既然说到优化,那我们就从没有优化的状态开始谈起。先使用最基本的流程实现一个UITableView(表视图),然后再谈优化,以及自定义。

  本文正文主要分一下三步:

一、使用最基本的流程来实现一个表视图。

二、通过滚动优化表视图中cell的创建使之节约内存。

三、自定义表视图中cell样式。

一、使用最基本的流程来实现一个表视图:

文章一开始说了UITableView这个控件中的列表的每一行是一个cell,所以我们先创建一个UITableView表视图,然后在datasource代理方法中返回cell的个数,以及创建cell:

1、创建UITableView控件myTableView,全局变量、设置代理、添加到当前view:

 #import "ViewController.h"

 @interface ViewController (){
UITableView *myTableView;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; myTableView = [[UITableView alloc]initWithFrame:CGRectMake(, , , )];
myTableView.backgroundColor = [UIColor lightGrayColor];
myTableView.delegate = self;
myTableView.dataSource = self; [self.view addSubview:myTableView];
}

2、在头文件中添加代理:

 //
// ViewController.h
// UITableView列表
//
// Created by mac on 16/4/15.
// Copyright © 2016年 mzw. All rights reserved.
// #import <UIKit/UIKit.h> //添加代理UITableViewDataSource和UITableViewDelegate
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @end

3、进入UITableViewDataSource中找到代理方法并粘贴到.m文件中,实现方法,其中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  返回一个数字,是cell的个数;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 返回的是UITableViewCell,所以我们就在这个函数中创建cell,并返回,这样就实现了代理方法。

具体代码:

 //myTableView的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 创建UITableViewCell
UITableViewCell *cell = [[UITableViewCell alloc]init];
// UITableViewCell自带textlabel,indexPath.row为行数
cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld行",indexPath.row];
return cell;
}

以上代码的第9行创建了UITableViewCell并初始化,第11行是给cell自带的textlabel设置文字的,这样的做法就是:需要多少个cell,就alloc多少次,如果软件需要的cell个数太多,会导致下拉时非常卡。但是在需要展示的数据量很小,也就是需要的cell不多的时候,可以使用这种方式。

通过这种方式实现的表视图模拟如下:

IOS开发中UITableView(表视图)的滚动优化及自定义Cell

二、通过优化表视图中cell的创建使之节约内存。

滚动优化的原理就是如果这个屏幕上能显示10个cell,那我就创建11个cell,不管有多少数据要现实,就用这11个cell。当用户上拉或下拉的时候,上面消失的cell将重新装入要显示的数据,然后显示在屏幕上,相当于总有一个在缓冲等待显示。通过这样的方式减少了cell的alloc次数,进而降低了内存开销,提高了系统性能。

要实现以上思路,只需要把一中的创建cell的代码换成一下语句:

 // //  2、使用队列池优化方式创建cell
static NSString *cellid = @"cell";//一个固定的标识
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}

三、自定义表视图中cell样式:

1、先新建一个继承自UITableViewCell的类,我这里创建的名称是myTableViewCell,创建的时候勾选创建nib文件。

2、打开创建好的nib文件,然后里面已经有一个cell了,我们采用拖拽的办法给这个cell里加入一个UIImageView,然后给里面放置一个图片,在这里我使用一张黄色的纯色图片,然后在cell的左边拖入一个buttom,右边拖入一个label,完成后如下图:

IOS开发中UITableView(表视图)的滚动优化及自定义Cell

3、在 myTableViewCell.h中定义两条语句

 @property(nonatmic, strong)IBOUTLET UIButton *mybutton;
@property(nonatmic, strong)IBOUTLET UILabel *mylabel;

写完这两句之后就可以在nib中拖线连接关系了,这个过程是最基本的,这里不说。

4、在ViewController.m中完成app 的主要功能,也就是表视图的实现:

 //
// ViewController.m
// UITableView使用nib拖动自定义
//
// Created by mac on 16/4/15.
// Copyright © 2016年 mzw. All rights reserved.
// #import "ViewController.h"
#import "myTableViewCell.h" @interface ViewController (){
UITableView *myTableView;
NSArray *contentArray;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; myTableView = [[UITableView alloc]initWithFrame:CGRectMake(, , , )]; myTableView.delegate = self;
myTableView.dataSource = self;
[self.view addSubview:myTableView]; // 用来为cell的mulabel赋值的数组
contentArray = @[@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine",@"ten",]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// 设置cell的数量为数组的元素个数
return contentArray.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 优化方式创建cell
static NSString *cellid = @"cell";
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (cell == nil) { // 使用绘制好的nib为cell赋值
cell = (myTableViewCell*)[[[NSBundle mainBundle]loadNibNamed:@"myTableViewCell" owner:self options:nil] objectAtIndex:]; // cell上按钮的button,为了从cell上的多个控件中区分出某个button
cell.mybutton.tag = ; // 为cell上的button添加方法
[cell.mybutton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside]; // 使用数组的内容为cell赋值
cell.mylabel.text = [contentArray objectAtIndex:[indexPath row]];
} return cell; } //拿到cell,然后根据tag拿到button然后输出buttn上的文字
-(void)haha:(UIButton*)sender{
// button在cell的content上,所以通过以下方法获取
myTableViewCell *cell =(myTableViewCell*)[[sender superview]superview];
UIButton *mybutton = (UIButton*)[cell viewWithTag:];
NSLog(@"%@",mybutton.titleLabel.text);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

模拟效果如下:

IOS开发中UITableView(表视图)的滚动优化及自定义Cell

  以上。