(一)系統(tǒng)默認的cell
1. cell 中默認有三個控件
imageView
textLabel
detailTextLabel
cell.accessoryType = 最右側(cè)view顯示的樣式
cell.accessoryView : 設置的時候, size 是起作用的, x 和 y 設置無效
2.cell 的樣式
UITableViewCellStyleDefault, : 圖片, textLabel
UITableViewCellStyleValue1, : 三個控件都顯示, detailTextLabel 顯示在最右側(cè)
UITableViewCellStyleValue2, : 只顯示兩個label, imageView 不顯示了
UITableViewCellStyleSubtitle
(三)cell的重用
tableViewCell 的重用機制
1.原始的方式(基本不用)
根據(jù)重用標識符, 到緩存池中查找, 如果找不到, 就重新實例化cell (需要我們手動實現(xiàn))
1. 定義重用標識符
2. 根據(jù)重用標識符到緩存中去找對應的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
3. 對取到的cell 進行判斷, 如果找不到就重新實例化cell
實例化的時候, 一定要設置重用標識符 :? identifier
if (nil == cell ) {
cell =? [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
2.注冊的方式
1.首先:定義重用標識符 identifier
2.其次:注冊
nibWithNibName : xib 的 文件名稱
bundle : 查找xib的目錄, 傳遞為nil , 默認就是當前app的安裝目錄
UINib *nib = [UINib nibWithNibName:@"GroupnCell" bundle:nil];
[_tabeView registerNib:nib forCellReuseIdentifier: identifier ];
如果找不到, 就重新實例化cell (不需要我們手動實現(xiàn), 而由系統(tǒng)幫我們做)
3.重用
[tableViewdequeueReusableCellWithIdentifier:identifier]
3.代碼示例
1.原始方式
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// 0.定義重用標識符
staticNSString*identifier =@"heroCell";
// 1.先到緩存池中去找cell根據(jù)標識符
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
//UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// 2.對取到的cell做判斷
if(nil== cell) {
//重新實例化一個cell
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];
NSLog(@"------- ");
}
return cell
}
2.注冊方式(官方推薦)
- (void)viewDidLoad {
[superviewDidLoad];
[self.tableView? registerClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell =[tableViewdequeueReusableCellWithIdentifier:@"cell":indexPath];
cell.textLabel.text=@"你好";
returncell;
}