UITableView的基本使用
- tableview滾動(dòng)的時(shí)候讓section的header停留效果
- 設(shè)置tableview的樣式為plain樣式
- 返回header的String即可
- cell的
backgroundColor
和backgroundView
- 兩者都可以改變cell的背景
-
backgroundView
的優(yōu)先級(jí)大于backgroundColor
- cell的
selectedBackgroundView
:設(shè)置cell的選中顯示的view - tableview的
separatorColor
:設(shè)置分割線的顏色 - tableview的
separatorStyle
:設(shè)置分割線的樣式
tableview的循環(huán)利用
- 循環(huán)利用的原理:
- 將移出屏幕的cell放在緩存池中,在上下滑動(dòng)過程中需要新的cell的時(shí)候?qū)⒕彺娉刂械拈e置cell拿出來使用
- 第一種是在緩存池中沒有閑置的cell的時(shí)候自己創(chuàng)建
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 被static修飾的局部變量:只會(huì)初始化一次,在整個(gè)程序運(yùn)行過程中,只有一份內(nèi)存
static NSString *ID = @"cell";
// 先根據(jù)cell的標(biāo)識(shí)去緩存池中查找可循環(huán)利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果cell為nil(緩存池找不到對(duì)應(yīng)的cell)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
return cell;
}
tableview的循環(huán)利用方式2
-
預(yù)先注冊(cè)cell
- (void)viewDidLoad { [super viewDidLoad]; // 注冊(cè)某個(gè)標(biāo)識(shí)對(duì)應(yīng)的cell類型 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
- 在返回cell的代理方法中去緩存池中取cell,如果不存在就會(huì)按照之前注冊(cè)的cell創(chuàng)建
```objc
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 去緩存池中查找cell,如果不存在就會(huì)按照之前注冊(cè)的cell創(chuàng)建
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
return cell;
}
tableview的循環(huán)利用方式3
在storyboard中先注冊(cè)好cell,在返回cell的方法中如果不存在就會(huì)按照storyboard中注冊(cè)的cell創(chuàng)建
拖一個(gè)tableviewController,選中tableview,顯示Dynamic Prototypes,這個(gè)是tableview的動(dòng)態(tài)創(chuàng)建cell,在這個(gè)模式下注冊(cè)的動(dòng)態(tài)的cell可以自動(dòng)創(chuàng)建
在下面的Prototypes cells中設(shè)置動(dòng)態(tài)cell的個(gè)數(shù),因?yàn)榭梢栽O(shè)置多個(gè)cell樣本
-
cell的一些設(shè)置
- 選中cell,Style設(shè)置cell的樣式,custom為自定義,自定義的才可以往上面拖控件
- Identifier為cell的重用標(biāo)識(shí)
在拖出的tableviewController中tableview已經(jīng)Prototypes cells有了1個(gè),但是在viewController中拖一個(gè)tableview在view上Prototypes cells顯示為0,這個(gè)時(shí)候調(diào)整Prototypes cells的值即可
-
代碼部分:
- (UITableViewCell *)cell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"cell"; // 先根據(jù)cell的標(biāo)識(shí)去緩存池中查找可循環(huán)利用的cell,查找不到會(huì)根據(jù)ID去storyboard中找對(duì)應(yīng)標(biāo)識(shí)的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row]; return cell;
}
###tableview常用的方法
- 插入行 : 1.增加數(shù)據(jù)源一個(gè)數(shù)據(jù) 2.tableView insert行
```objc
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
- 刪除行 :1. 先要?jiǎng)h除數(shù)據(jù)源的一個(gè)數(shù)據(jù) 2.tableView delete行
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
-
更新一行的數(shù)據(jù) : 1.先更新數(shù)據(jù) 2.tableView reload 行
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
以上的刪除、增加、更新一行的方法在執(zhí)行的時(shí)候要保證數(shù)據(jù)源的個(gè)數(shù)和cell的個(gè)數(shù)相匹配,否則會(huì)報(bào)錯(cuò)
tableview的編譯模式
- 實(shí)現(xiàn)tableview的左滑,實(shí)現(xiàn)下面的代理方法即可
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) { // 點(diǎn)擊了“刪除”
} else if (editingStyle == UITableViewCellEditingStyleInsert) { // 點(diǎn)擊了+
}
}
tableview的編譯模式
先設(shè)置編譯模式
-
默認(rèn)都是刪除按鈕
[self.tableView setEditing:YES animated:YES];
-
實(shí)現(xiàn)下面的代理方法可以指定特定行的編輯模式是增加還是刪除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}
##tableview的批量操作
- 先設(shè)置tableview的允許在編輯模式進(jìn)行多選操作:
```objc
self.tableView.allowsMultipleSelectionDuringEditing = YES;
- 進(jìn)入編譯模式(這個(gè)時(shí)候就可以多選了)
[self.tableView setEditing:YES animated:YES];
- 獲得選中的多行
self.tableView.allowsMultipleSelectionDuringEditing = YES;
tableView在有數(shù)據(jù)的行才顯示分割線
- 第一種做法是講
tableView
的風(fēng)格改為group
- 第二種做法是給
tableView
設(shè)置一個(gè)footView
,footView
可以是UIView
直接alloc
init
之后的即可
cell的循環(huán)利用
storyboard方式
在storyboard中自定義cell并給cell綁定標(biāo)識(shí)@"cell"
在返回cell的代理方法中使用@"cell"標(biāo)識(shí)讓tableView去取cell,如果取到的cell為空,那么會(huì)檢查storyboard中是否有相同方式的cell,如果有則創(chuàng)建一個(gè)
xib方式
在xib中設(shè)置cell的標(biāo)識(shí)
使用tableView注冊(cè)一個(gè)xib的cell
在返回cell的代理方法中使用@"cell"標(biāo)識(shí)讓tableView去取cell,如果取到的cell為空,那么會(huì)檢查tableview是否曾經(jīng)注冊(cè)過該cell,如果有則創(chuàng)建一個(gè)
-
注意的是如果一個(gè)xib的cell有一個(gè)類,例如RHCell,在注冊(cè)的時(shí)候不要使用RHCell類去注冊(cè),如果使用該類去注冊(cè)在創(chuàng)建cell的時(shí)候就不會(huì)去加載xib中的內(nèi)容。可能創(chuàng)建的方式是[[RHCell alloc] init];
- (void)viewDidLoad{ [super viewDidLoad]; [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([RHWeiboCell class]) bundle:nil] forCellReuseIdentifier:@"cell"]; }
代碼方式
自定義cell,在cell中添加子控件,和以前用過的cell自定義一樣
-
注冊(cè)cell,由于是代碼的方式在注冊(cè)的時(shí)候使用類注冊(cè)即可
- (void)viewDidLoad{ [super viewDidLoad]; [self.tableview registerClass:[RHCell class] forCellReuseIdentifier:@"cell"];
}
```objc
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
}
return self;
}