IOS 程序猿 UITbleView 篇
UITableView
在iOS中顯示數(shù)據(jù)列表最常用的一個(gè)控件,表格控件UITableView,它支持垂直滾動(dòng)它內(nèi)置了兩種樣式:UITableViewStylePlain(平鋪),UITableViewStyleGrouped(分組)
那么TableView是怎么展示數(shù)據(jù)的呢?
UITableView需要一個(gè)數(shù)據(jù)源(dataSource)來(lái)顯示數(shù)據(jù),UITableView會(huì)向數(shù)據(jù)源查詢(xún)一共有多少行數(shù)據(jù)以及每一行顯示什么數(shù)據(jù)等。沒(méi)有設(shè)置數(shù)據(jù)
源的UITableView只是個(gè)空殼。凡是遵守UITableViewDataSource協(xié)議的OC對(duì)象,都可以是UITableView的數(shù)據(jù)源通常都要為UITableView設(shè)置代
理對(duì)象(delegate),以便在UITableView觸發(fā)一下事件時(shí)做出相應(yīng)的處理,比如選中了某一行。凡是遵守了UITableViewDelegate協(xié)議的OC對(duì)象,都
可以是UITableView的代理對(duì)象,一般會(huì)讓控制器充當(dāng)UITableView的dataSource和delegate。
?廢話不多說(shuō) 看下面 ↓
<UITableViewDataSource,UITableViewDelegate>的方法:
//有多少組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//第section組頭部控件有多高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//第section組有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//indexPath這行的cell有多高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//indexPath這行的cell長(zhǎng)什么樣子
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//第section組頭部顯示什么控件
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//當(dāng)調(diào)用reloadData的時(shí)候,會(huì)重新刷新調(diào)用數(shù)據(jù)源內(nèi)所有方法,其他事情都不會(huì)做
?[self reloadData]
?
?//這個(gè)方法只有在一開(kāi)始有多少條數(shù)據(jù)會(huì)算多少個(gè)高度,這個(gè)方法只會(huì)調(diào)用一次,但是每次reloadData的時(shí)候也會(huì)調(diào)用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
?-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右側(cè)索引
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行點(diǎn)擊事件
?NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //獲得被選中的indexPath可以得到section,row
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; //刷新table指定行的數(shù)據(jù)
?[self.tableView reloadData]; //刷新table所有行的數(shù)據(jù)
?
UITableView常用屬性:
// 初始化表格(并設(shè)置位置,寬高,樣式)。
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
??? 分隔線屬性
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //UITableViewCellSeparatorStyleNone;
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔線
tableView.separatorColor = [UIColor lightGrayColor];
???
? ?// 條目多選
tableView.allowsMultipleSelection = YES;
????
??? // 設(shè)置標(biāo)題行高
[_tableView setSectionHeaderHeight:kHeaderHeight];
[_tableView setSectionFooterHeight:0];
?
??? // 設(shè)置表格行高
?[_tableView setRowHeight:50];
?
//設(shè)置背景色
self.tableView.backgroundView? 優(yōu)先級(jí)高,如果要設(shè)置backgroundColor的時(shí)候要先把view設(shè)置為nil
self.tableView.backgroundColor = [UIColor redColor];
?
//在tableView的頭部或者尾部添加view,footerView寬度是不用設(shè)置的
?????? xxxView.bounds = CGRectMake(0,0,0,height);
??? self.tableView.tableFooterView =xxxView;
?????? self.tableView.tableHeaderView =xxxView;
?
增加tableview滾動(dòng)區(qū)域
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);?
UITableViewCell
/* lUITableView的每一行都是一個(gè)UITableViewCell,通過(guò)dataSource的tableView:cellForRowAtIndexPath:方法來(lái)初始化每一行 UITableViewCell是UIView的子類(lèi),內(nèi)部有個(gè)默認(rèn)的子視圖:contentView。contentView是UITableViewCell所顯示內(nèi)容的父視圖,并負(fù)責(zé)顯示一些輔助指示視圖。輔助指示視圖的作用是顯示一個(gè)表示動(dòng)作的圖標(biāo),可以通過(guò)設(shè)置UITableViewCell的accessoryType來(lái)顯示,默認(rèn)是UITableViewCellAccessoryNone *///創(chuàng)建UITableViewCell????????
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];// 清空標(biāo)簽背景顏色
cell.backgroundView =xx; //設(shè)置背景圖片
cell.backgroundVColor =xx;//設(shè)置背景顏色
cell.selectedBackgroundView = selectedBgView; //設(shè)置選中時(shí)的背景顏色
cell.accessoryView = xxxView; //設(shè)置右邊視圖
[cell setAccessoryType:UITableViewCellAccessoryNone]; //設(shè)置右側(cè)箭頭?
[self setSelectionStyle:UITableViewCellSelectionStyleNone]; //選中樣式
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//設(shè)置cell的高度
?
/*
contentView下默認(rèn)有3個(gè)子視圖,其中的2個(gè)是UILabel,通過(guò)textLabel和detailTextLabel屬性訪問(wèn),第3個(gè)是UIImageView,通過(guò)imageView屬性訪問(wèn).
UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle
?*/
#pragma mark - 重新調(diào)整UITalbleViewCell中的控件布局
- (void)layoutSubviews
{
[super layoutSubviews];
…
}
cell 里面還有一個(gè)contentView
UITableViewCell表格優(yōu)化
UITableViewCell對(duì)象的重用原理:
liOS設(shè)備的內(nèi)存有限,如果用UITableView顯示成千上萬(wàn)條數(shù)據(jù),就需要成千上萬(wàn)個(gè)UITableViewCell對(duì)象的話,那將會(huì)耗盡iOS設(shè)備的內(nèi)存。要解決 該問(wèn)題,需要重用UITableViewCell對(duì)象 重用原理:當(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ù)配置這個(gè)UITableViewCell,然后返回給UITableView,重新顯示到窗口中,從而避免創(chuàng)建新對(duì)象 l還有一個(gè)非常重要的問(wèn)題:有時(shí)候需要自定義UITableViewCell(用一個(gè)子類(lèi)繼承UITableViewCell),而且每一行用的不一定是同一種 UITableViewCell(如短信聊天布局),所以一個(gè)UITableView可能擁有不同類(lèi)型的UITableViewCell,對(duì)象池中也會(huì)有很多不同類(lèi)型的 UITableViewCell,那么UITableView在重用UITableViewCell時(shí)可能會(huì)得到錯(cuò)誤類(lèi)型的UITableViewCell 解決方案:UITableViewCell有個(gè)NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時(shí)候傳入一個(gè)特定的字符串標(biāo)識(shí)來(lái)設(shè)置 reuseIdentifier(一般用UITableViewCell的類(lèi)名)。當(dāng)UITableView要求dataSource返回UITableViewCell時(shí),先通過(guò)一個(gè)字符串標(biāo)識(shí)到對(duì)象池中查 找對(duì)應(yīng)類(lèi)型的UITableViewCell對(duì)象,如果有,就重用,如果沒(méi)有,就傳入這個(gè)字符串標(biāo)識(shí)來(lái)初始化一個(gè)UITableViewCell對(duì)象?
單元格優(yōu)化
/*
?1. 標(biāo)示符統(tǒng)一,使用static的目的可以保證表格標(biāo)示符永遠(yuǎn)只有一個(gè)
?2. 首先在緩沖池中找名為"myCell"的單元格對(duì)象
?3. 如果沒(méi)有找到,實(shí)例化一個(gè)新的cell
*/
?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
??? static NSString *cellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//使用這種方法不用判斷下面的cell
??? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
??? if (cell == nil) {???????
??????? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
? ?}
return cell;
}
?
表格的編輯模式
刪除、插入
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;? 開(kāi)啟表格編輯狀態(tài)
?
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? ? ?// 返回表格編輯編輯樣式。不實(shí)現(xiàn)默認(rèn)都是刪除
return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert
}
?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
??? ??// 根據(jù)editingStyle處理是刪除還是添加操作完成刪除、插入操作刷新表格
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
?
-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
}
?
移動(dòng)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
sourceIndexPath 移動(dòng)的行????
destinationIndexPath 目標(biāo)的行
?
自定義表格行UITableViewCell
storyboard方式創(chuàng)建:
直接拖到UITableView里面設(shè)置UITableViewCell
注意:
1.通過(guò)XIB或者Storyboard自定義單元格時(shí),在xib和Storyboard里面需要指定單元格的可重用標(biāo)示符Identifier
?
2.注意表格的優(yōu)化中的差別
在Storyboard中兩者等效
xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
xxCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
?
在xib文件中有差別:
第一種情況,只能在iOS 6以上使用,如果在viewDidLoad注冊(cè)了nib文件,并且指定了“單元格”的可重用標(biāo)示符,那么????
???? dequeueReusableCellWithIdentifier:
???? dequeueReusableCellWithIdentifier:forIndexPath:
???? 方法是等效的。如果在viewDidLoad中注冊(cè)了nib文件,表格緩沖池中的管理,有系統(tǒng)接管!
?
第二種情況,是在iOS 4以上均可以使用,如果沒(méi)有在viewDidLoad注冊(cè)nib文件,那么,只能使用
???? dequeueReusableCellWithIdentifier:并且需要判斷cell沒(méi)有被實(shí)例化,并做相應(yīng)的處理
?
?
在代碼創(chuàng)建中差別:
用代碼創(chuàng)建cell中的處理和nib一樣,注冊(cè)了cell就有系統(tǒng)接管并且可以用帶forIndexPath的方法,沒(méi)有注冊(cè)就要自己去實(shí)例化cell,不能用帶forIndexPath的方法
[tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];
?
xib方式創(chuàng)建:
// 注冊(cè)Identifier
- (void)viewDidLoad{
??? [super viewDidLoad];
??? /**
???? 注意:以下幾句注冊(cè)XIB的代碼,一定要在viewDidLoad中!
???? 注冊(cè)XIB文件,獲得根視圖,并且轉(zhuǎn)換成TableView,為tableView注冊(cè)xib
???? Identifier名要在xib文件中定義,并且保持一致
???? **/
??? UINib *nib = [UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];
??? UITableView *tableView = (UITableView *)self.view;
??? [tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];???
}
?
// 沒(méi)有注冊(cè)Identifier只能使用下面方法
static NSString *CellIdentifier = @"xxxCell";
XxxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
??????? cell = [[XxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
??????? NSBundle *bundle = [NSBundle mainBundle];
??????? NSArray *array = [bundle loadNibNamed:@"XxxCell" owner:nil options:nil];
??????? cell = [array lastObject];
}
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/blackCat-tommy/p/3572721.html
總結(jié)
以上是生活随笔為你收集整理的IOS 程序猿 UITbleView 篇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Revit API创建标高,单位转换
- 下一篇: 代码编辑器Sublime Text 3