iOS開(kāi)發(fā)中UITableview控件的基本使用及性能優(yōu)化方法
UITableview控件基本使用
一、一個(gè)簡(jiǎn)單的英雄展示程序
NJHero.h文件代碼(字典轉(zhuǎn)模型)
#import <Foundation/Foundation.h>
@interface NJHero : NSObject
/**
* 頭像
*/
@property (nonatomic, copy) NSString *icon;
/**
* 名稱
*/
@property (nonatomic, copy) NSString *name;
/**
* 描述
*/
@property (nonatomic, copy) NSString *intro;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)heroWithDict:(NSDictionary *)dict;
@end
NJViewController.m文件代碼
#import "NJViewController.h"
#import "NJHero.h"
@interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
* 保存所有的英雄數(shù)據(jù)
*/
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation NJViewController
#pragma mark - 懶加載
- (NSArray *)heros
{
if (_heros == nil) {
// 1.獲得全路徑
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
// 2.更具全路徑加載數(shù)據(jù)
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
// 3.字典轉(zhuǎn)模型
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
NJHero *hero = [NJHero heroWithDict:dict];
[models addObject:hero];
}
// 4.賦值數(shù)據(jù)
_heros = [models copy];
}
// 4.返回?cái)?shù)據(jù)
return _heros;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 設(shè)置Cell的高度
// 當(dāng)每一行的cell高度一致的時(shí)候使用屬性設(shè)置cell的高度
self.tableView.rowHeight = 60;
self.tableView.delegate = self;
}
#pragma mark - UITableViewDataSource
// 返回多少組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// 返回每一組有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
// 返回哪一組的哪一行顯示什么內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.創(chuàng)建CELL
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// 2.設(shè)置數(shù)據(jù)
// 2.1取出對(duì)應(yīng)行的模型
NJHero *hero = self.heros[indexPath.row];
// 2.2賦值對(duì)應(yīng)的數(shù)據(jù)
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
// 3.返回cell
return cell;
}
#pragma mark - UITableViewDelegate
/*
// 當(dāng)每一行的cell的高度不一致的時(shí)候就使用代理方法設(shè)置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (1 == indexPath.row) {
return 180;
}
return 44;
}
*/
#pragma mark - 控制狀態(tài)欄是否顯示
/**
* 返回YES代表隱藏狀態(tài)欄, NO相反
*/
- (BOOL)prefersStatusBarHidden
{
return YES;
}
@end
實(shí)現(xiàn)效果:
代碼注意點(diǎn):
(1)在字典轉(zhuǎn)模型的代碼處用下面的代碼,為可變數(shù)組分配dictArray.count個(gè)存儲(chǔ)空間,可以提高程序的性能
NSMutableArray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];
(2)設(shè)置cell的高度
有三種辦法可以設(shè)置cell的高度
1) 可以在初始加載方法中設(shè)置,self.tableView.rowHeight = 60;這適用于當(dāng)每一行的cell高度一致的時(shí)候,使用屬性設(shè)置cell的高度。
2)在storyboard中設(shè)置,適用于高度一致
3)當(dāng)每一行的cell的高度不一致的時(shí)候就使用代理方法設(shè)置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (1 == indexPath.row) {
return 180;
}
return 44;
}
二、cell的一些屬性
代碼示例:
#import "NJViewController.h"
#import "NJHero.h"
@interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
* 保存所有的英雄數(shù)據(jù)
*/
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation NJViewController
#pragma mark - 懶加載
- (NSArray *)heros
{
if (_heros == nil) {
// 1.獲得全路徑
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
// 2.更具全路徑加載數(shù)據(jù)
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
// 3.字典轉(zhuǎn)模型
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
NJHero *hero = [NJHero heroWithDict:dict];
[models addObject:hero];
}
// 4.賦值數(shù)據(jù)
_heros = [models copy];
}
// 4.返回?cái)?shù)據(jù)
return _heros;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 設(shè)置Cell的高度
// 當(dāng)每一行的cell高度一致的時(shí)候使用屬性設(shè)置cell的高度
self.tableView.rowHeight = 60;
self.tableView.delegate = self;
}
#pragma mark - UITableViewDataSource
// 返回多少組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// 返回每一組有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
// 返回哪一組的哪一行顯示什么內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.創(chuàng)建CELL
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// 2.設(shè)置數(shù)據(jù)
// 2.1取出對(duì)應(yīng)行的模型
NJHero *hero = self.heros[indexPath.row];
// 2.2賦值對(duì)應(yīng)的數(shù)據(jù)
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
// 2.3設(shè)置cell的輔助視圖
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (0 == indexPath.row) {
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
}else
{
cell.accessoryView = [[UISwitch alloc] init];
}
// UIButton *btn = [[UIButton alloc] init];
// btn.backgroundColor = [UIColor redColor];
// cell.accessoryView = btn;
// 2.4設(shè)置cell的背景顏色
cell.backgroundColor = [UIColor blueColor];
// 設(shè)置默認(rèn)狀態(tài)的背景
// UIView *view = [[UIView alloc] init];
// view.backgroundColor = [UIColor blueColor];
// cell.backgroundView = view;
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]];
cell.backgroundView = iv;
// 設(shè)置選中狀態(tài)的背景
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = view2;
// 3.返回cell
return cell;
}
#pragma mark - 控制狀態(tài)欄是否顯示
/**
* 返回YES代表隱藏狀態(tài)欄, NO相反
*/
- (BOOL)prefersStatusBarHidden
{
return YES;
}
@end
實(shí)現(xiàn)效果:
cell的一些屬性:
(1)設(shè)置cell的輔助視圖,設(shè)置cell.accessoryView(系統(tǒng)提供了枚舉型,也可以自定義@父類指針指向子類對(duì)象);
(2)設(shè)置cell的背景顏色,有兩種方式可以設(shè)置cell的背景顏色:
通過(guò)backgroundColor 和 backgroundView都可以設(shè)置cell的背景。但是backgroundView 的優(yōu)先級(jí)比 backgroundColor的高,所以如果同時(shí)設(shè)置了backgroundColor和backgroundView, 那么backgroundView會(huì)蓋住backgroundColor
示例:cell.backgroundColor = [UIColorblueColor];
(3)設(shè)置cell默認(rèn)狀態(tài)的背景
示例1:
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blueColor];
cell.backgroundView = view;
示例2:
UIImageView *iv = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"buttondelete"]];
cell.backgroundView = iv;(父類指針指向子類對(duì)象,可以使用圖片用簡(jiǎn)單的操作設(shè)置絢麗的效果)
(4)設(shè)置cell選中狀態(tài)的背景
示例:
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColorpurpleColor];
cell.selectedBackgroundView = view2;
三、tableview的一些屬性
代碼示例:
#import "NJViewController.h"
@interface NJViewController ()<UITableViewDataSource>
@end
@implementation NJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.創(chuàng)建tableview
UITableView *tableview = [[UITableView alloc] init];
tableview.frame = self.view.bounds;
// 2.設(shè)置數(shù)據(jù)源
tableview.dataSource =self;
// 3.添加tableview到view
[self.view addSubview:tableview];
// 4.設(shè)置分割線樣式
// tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
// 5.設(shè)置分割線顏色
接收的參數(shù)是顏色的比例值
tableview.separatorColor = [UIColor colorWithRed:0/255.0 green:255/255.0 blue:0/255.0 alpha:255/255.0];
// 設(shè)置tableview的頭部視圖
tableview.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
tableview.tableFooterView = [[UISwitch alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.創(chuàng)建cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// 2.設(shè)置cell的數(shù)據(jù)
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row ];
// 3.返回cell
return cell;
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
@end
實(shí)現(xiàn)效果:
tableview的一些屬性:
(1)設(shè)置分割樣式(tableview.separatorStyle),這是個(gè)枚舉類型
(2)設(shè)置分割線的顏色,可以直接使用系統(tǒng)給出的顏色,如果系統(tǒng)給定的顏色不能滿足需求時(shí),也可以自定義。
補(bǔ)充:顏色分為24位和32位的,如下
24bit顏色
R 8bit 0 ~ 255
G 8bit 0 ~ 255
B 8bit 0 ~ 255
32bit顏色
A 8bit 0 ~ 255(tou)
R 8bit
G 8bit
B 8bit
#ff ff ff 白色
#00 00 00 黑色
#ff 00 00 紅色
#255 00 00
設(shè)置為自定義顏色的實(shí)例:
//接收的參數(shù)是顏色的比例值
(3)設(shè)置頂部和底部視圖
tableview.tableHeaderView //頂部
tableview.tableFooterView //底部
UITableviewcell的性能問(wèn)題
一、UITableviewcell的一些介紹
UITableView的每一行都是一個(gè)UITableViewCell,通過(guò)dataSource的 tableView:cellForRowAtIndexPath:方法來(lái)初始化每⼀行
UITableViewCell內(nèi)部有個(gè)默認(rèn)的子視圖:contentView,contentView是UITableViewCell所顯示內(nèi)容的父視圖,可顯示一些輔助指示視圖
輔助指示視圖的作⽤是顯示一個(gè)表示動(dòng)作的圖標(biāo),可以通過(guò)設(shè)置UITableViewCell的 accessoryType來(lái)顯示,默認(rèn)是UITableViewCellAccessoryNone(不顯⽰示輔助指⽰示視圖), 其他值如下:
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
還可以通過(guò)cell的accessoryView屬性來(lái)自定義輔助指示視圖(⽐如往右邊放一個(gè)開(kāi)關(guān))
二、問(wèn)題
cell的工作:在程序執(zhí)行的時(shí)候,能看到多少條,它就創(chuàng)建多少條數(shù)據(jù),如果視圖滾動(dòng)那么再創(chuàng)建新顯示的內(nèi)容。(系統(tǒng)自動(dòng)調(diào)用)。即當(dāng)一個(gè)cell出現(xiàn)在視野范圍內(nèi)的時(shí)候,就會(huì)調(diào)用創(chuàng)建一個(gè)cell。這樣的邏輯看上去沒(méi)有什么問(wèn)題,但是真的沒(méi)有任何問(wèn)題嗎?
當(dāng)創(chuàng)建調(diào)用的時(shí)候,我們使用nslog打印消息,并打印創(chuàng)建的cell的地址。我們發(fā)現(xiàn)如果數(shù)據(jù)量非常大,用戶在短時(shí)間內(nèi)來(lái)回滾動(dòng)的話,那么會(huì)創(chuàng)建大量的cell,一直開(kāi)辟空間,且如果是往回滾,通過(guò)打印地址,我們會(huì)發(fā)現(xiàn)它并沒(méi)有重用之前已經(jīng)創(chuàng)建的cell,而是重新創(chuàng)建,開(kāi)辟新的存儲(chǔ)空間。
那有沒(méi)有什么好的解決辦法呢?
三、cell的重用原理
(1) iOS設(shè)備的內(nèi)存有限,如果用UITableView顯示成千上萬(wàn)條數(shù)據(jù),就需要成千上萬(wàn) 個(gè)UITableViewCell對(duì)象的話,那將會(huì)耗盡iOS設(shè)備的內(nèi)存。要解決該問(wèn)題,需要重用UITableViewCell對(duì)象
(2)重⽤原理:當(dāng)滾動(dòng)列表時(shí),部分UITableViewCell會(huì)移出窗口,UITableView會(huì)將窗口外的UITableViewCell放入一個(gè)對(duì)象池中,等待重用。當(dāng)UITableView要求dataSource返回 UITableViewCell時(shí),dataSource會(huì)先查看這個(gè)對(duì)象池,如果池中有未使用的 UITableViewCell,dataSource則會(huì)用新的數(shù)據(jù)來(lái)配置這個(gè)UITableViewCell,然后返回給 UITableView,重新顯示到窗口中,從而避免創(chuàng)建新對(duì)象 。這樣可以讓創(chuàng)建的cell的數(shù)量維持在很低的水平,如果一個(gè)窗口中只能顯示5個(gè)cell,那么cell重用之后,只需要?jiǎng)?chuàng)建6個(gè)cell就夠了。
(3)注意點(diǎn):還有⼀個(gè)非常重要的問(wèn)題:有時(shí)候需要自定義UITableViewCell(用⼀個(gè)子類繼 承UITableViewCell),而且每⼀行⽤的不一定是同一種UITableViewCell,所以一 個(gè)UITableView可能擁有不同類型的UITableViewCell,對(duì)象池中也會(huì)有很多不同類型的 UITableViewCell,那么UITableView在重⽤用UITableViewCell時(shí)可能會(huì)得到錯(cuò)誤類型的 UITableViewCell
解決⽅方案:UITableViewCell有個(gè)NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時(shí)候傳入一個(gè)特定的字符串標(biāo)識(shí)來(lái)設(shè)置reuseIdentifier(一般用UITableViewCell的類名)。當(dāng)UITableView要求dataSource返回UITableViewCell時(shí),先 通過(guò)一個(gè)字符串標(biāo)識(shí)到對(duì)象池中查找對(duì)應(yīng)類型的UITableViewCell對(duì)象,如果有,就重用,如果沒(méi)有,就傳入這個(gè)字符串標(biāo)識(shí)來(lái)初始化⼀一個(gè)UITableViewCell對(duì)象。
圖片示例:
說(shuō)明:一個(gè)窗口放得下(可視)三個(gè)cell,整個(gè)程序只需要?jiǎng)?chuàng)建4個(gè)該類型的cell即可。
四、cell的優(yōu)化代碼
代碼示例:
#import "NJViewController.h"
#import "NJHero.h"
// #define ID @"ABC"
@interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
* 保存所有的英雄數(shù)據(jù)
*/
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation NJViewController
#pragma mark - 懶加載
- (NSArray *)heros
{
if (_heros == nil) {
// 1.獲得全路徑
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
// 2.更具全路徑加載數(shù)據(jù)
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
// 3.字典轉(zhuǎn)模型
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
NJHero *hero = [NJHero heroWithDict:dict];
[models addObject:hero];
}
// 4.賦值數(shù)據(jù)
_heros = [models copy];
}
// 4.返回?cái)?shù)據(jù)
return _heros;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 設(shè)置Cell的高度
// 當(dāng)每一行的cell高度一致的時(shí)候使用屬性設(shè)置cell的高度
self.tableView.rowHeight = 160;
}
#pragma mark - UITableViewDataSource
// 返回多少組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// 返回每一組有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
// 當(dāng)一個(gè)cell出現(xiàn)視野范圍內(nèi)的時(shí)候就會(huì)調(diào)用
// 返回哪一組的哪一行顯示什么內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定義變量保存重用標(biāo)記的值
static NSString *identifier = @"hero";
// 1.先去緩存池中查找是否有滿足條件的Cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 2.如果緩存池中沒(méi)有符合條件的cell,就自己創(chuàng)建一個(gè)Cell
if (cell == nil) {
// 3.創(chuàng)建Cell, 并且設(shè)置一個(gè)唯一的標(biāo)記
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
NSLog(@"創(chuàng)建一個(gè)新的Cell");
}
// 4.給cell設(shè)置數(shù)據(jù)
NJHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
// NSLog(@"%@ - %d - %p", hero.name, indexPath.row, cell);
// 3.返回cell
return cell;
}
#pragma mark - 控制狀態(tài)欄是否顯示
/**
* 返回YES代表隱藏狀態(tài)欄, NO相反
*/
- (BOOL)prefersStatusBarHidden
{
return YES;
}
@end
緩存優(yōu)化的思路:
(1)先去緩存池中查找是否有滿足條件的cell,若有那就直接拿來(lái)
(2)若沒(méi)有,就自己創(chuàng)建一個(gè)cell
(3)創(chuàng)建cell,并且設(shè)置一個(gè)唯一的標(biāo)記(把屬于“”的給蓋個(gè)章)
(4)給cell設(shè)置數(shù)據(jù)
注意點(diǎn):
定義變量用來(lái)保存重用標(biāo)記的值,這里不推薦使用宏(#define來(lái)處理),因?yàn)樵撟兞恐辉谶@個(gè)作用域的內(nèi)部使用,且如果使用宏定義的話,定義和使用位置太分散,不利于閱讀程序。由于其值不變,沒(méi)有必要每次都開(kāi)辟一次,所以用static定義為一個(gè)靜態(tài)變量。
相關(guān)文章
Unity移動(dòng)端的復(fù)制要這么寫(xiě)示例代碼
這篇文章主要給大家介紹了關(guān)于Unity移動(dòng)端的復(fù)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08IOS開(kāi)發(fā)OC代碼中創(chuàng)建Swift編寫(xiě)的視圖控制器
這篇文章主要介紹了IOS開(kāi)發(fā)OC代碼中創(chuàng)建Swift編寫(xiě)的視圖控制器的相關(guān)資料,需要的朋友可以參考下2017-06-06iOS省市二級(jí)聯(lián)動(dòng)的數(shù)據(jù)組織PHP版
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)之"省市"二級(jí)聯(lián)動(dòng)的數(shù)據(jù)組織PHP版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09iOS實(shí)現(xiàn)簡(jiǎn)單的頭部縮放功能
這篇文章主要介紹了iOS 簡(jiǎn)單的頭部縮放效果,頭部伴隨模糊效果放大縮小,并在一定位置時(shí)懸停充當(dāng)導(dǎo)航欄,本文給大家提供實(shí)現(xiàn)思路,需要的朋友可以參考下2018-08-08iOS CAEmitterLayer實(shí)現(xiàn)粒子發(fā)射動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了iOS CAEmitterLayer 實(shí)現(xiàn)粒子發(fā)射動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06