UITableView
數(shù)據(jù)源的常用方法
此方法設(shè)置 cell 的組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
每一組有多少行數(shù)據(jù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
每一行顯示什么內(nèi)容梢夯,返回 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置每一組的頭部信息
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
設(shè)置每一組的尾部信息
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
-
tabelView常見屬性
// 設(shè)置每一行cell的高度 self.tableView.rowHeight = 100; // 設(shè)置每一組頭部的高度 self.tableView.sectionHeaderHeight = 50; // 設(shè)置每一組尾部的高度 self.tableView.sectionFooterHeight = 50; // 設(shè)置分割線顏色 self.tableView.separatorColor = [UIColor redColor]; // 設(shè)置分割線樣式 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 設(shè)置表頭控件 self.tableView.tableHeaderView = [[UISwitch alloc] init]; // 設(shè)置表尾控件 self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd]; // 設(shè)置右邊索引文字的顏色 self.tableView.sectionIndexColor = [UIColor redColor]; // 設(shè)置右邊索引文字的背景色 self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
cell
UITableView的每一行都是一個(gè)UITableViewCell痊乾,通過dataSource的tableView:cellForRowAtIndexPath:方法來初始化每一行
UITableViewCell內(nèi)部有個(gè)默認(rèn)的子視圖:contentView,contentView是UITableViewCell所顯示內(nèi)容的父視圖霍弹,可顯示一些輔助指示視圖
輔助指示視圖的作用是顯示一個(gè)表示動(dòng)作的圖標(biāo)绕沈,可以通過設(shè)置UITableViewCell的accessoryType來顯示篮绿,默認(rèn)是UITableViewCellAccessoryNone(不顯示輔助指示視圖)
可以通過cell的accessoryView屬性來自定義輔助指示視圖(比如往右邊放一個(gè)開關(guān))
-
傳統(tǒng)的寫法
/** * 每當(dāng)有一個(gè)cell要進(jìn)入視野范圍內(nèi)纹份,就會(huì)調(diào)用一次 */ - (UITableViewCell *)tableView:(UITableView *)tableView cell ForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"wine"; // 1.先去緩存池中查找可循環(huán)利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.如果緩存池中沒有可循環(huán)利用的cell if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 3.設(shè)置數(shù)據(jù) cell.textLabel.text = [NSString stringWithFormat:@"%zd行的數(shù)據(jù)", indexPath.row]; return cell;
}
```
-
新的寫法(注冊(cè)cell)
NSString *ID = @"wine"; - (void)viewDidLoad { [super viewDidLoad]; // 注冊(cè)某個(gè)重用標(biāo)識(shí) 對(duì)應(yīng)的 Cell類型 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.先去緩存池中查找可循環(huán)利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.設(shè)置數(shù)據(jù)
cell.textLabel.text = [NSString stringWithFormat:@"%zd行的數(shù)據(jù)", indexPath.row];
return cell;
}
```
自定義 cell
-
等高 cell 代碼
- 新建一個(gè)繼承自
UITableViewCell
的子類 - 重寫新建子類的
-initWithStyle:reuseIdentifier:
方法在這個(gè)方法中添加所有需要顯示的子控件。給子控件做一些初始化設(shè)置(設(shè)置字體旗们、文字顏色等) - 重寫
-layoutSubviews
方法一定要調(diào)用[superlayoutSubviews]
在這個(gè)方法中計(jì)算和設(shè)置所有子控件的frame - 提供模型屬性,設(shè)置模型的數(shù)據(jù)构灸,在 set 方法中
- 注冊(cè) cell上渴。。喜颁。下面方法是從 xib 中 zuce
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MTTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
- 新建一個(gè)繼承自
-
不等高 cell
- 在模型內(nèi)部提供可設(shè)置 cell高度 的的 frame屬性稠氮,
@interface XMGStatus : NSObject @property (nonatomic, assign) CGRect iconFrame; /** cell的高度 */ @property (nonatomic, assign) CGFloat cellHeight; @end
-
在
model.m
中重寫模型cellHeight屬性的get方法- (CGFloat)cellHeight { if (_cellHeight == 0) { // ... 計(jì)算所有子控件的frame、cell的高度 } return _cellHeight; }
-
在控制器中實(shí)現(xiàn)返回 cell 高度的代理方法半开,heightforrow
// 返回每一行cell的具體高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { SBStatus *status = self.statuses[indexPath.row]; return status.cellHeight; }
給 cell 模型傳值
新建一個(gè)繼承自
UITableViewCell
的子類隔披,比如SBStatusCell,并且提供一個(gè)模型屬性在SBStatusCell.m中重寫模型屬性的set方法
-
文字尺寸的計(jì)算
- 計(jì)算文字所占據(jù)的尺寸
NSDictionary *nameAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:17]}; CGSize nameSize = [self.name sizeWithAttributes:nameAttrs];
- 計(jì)算一段文字的尺寸
CGSize textMaxSize = CGSizeMake(textW, MAXFLOAT); NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14]}; CGFloat textH = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;
- 計(jì)算屏幕的寬
[UIScreen mainScreen].bounds.size.width
- 連線修改約束
- 定義一個(gè)高度約束且連線到 storyboard
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *picHeight;
- 然后在模型設(shè)置控件的 set 方法中修改
self.picHeight.constant = 100;
- 定義一個(gè)高度約束且連線到 storyboard
- cellheight 不等高的估算和自動(dòng)計(jì)算
- 程序一開始就會(huì)吧 tabelViewl 的所有cell高度算出來寂拆,確定 contengSize 的大小奢米,從而計(jì)算出滾動(dòng)條的長(zhǎng)度。所以這樣的性能不好纠永,所以蘋果推出了
估算高度
鬓长。 - 告訴tabelview的 cell的估算高度,就可以使heightforrow方法的調(diào)用次數(shù)減少
- 設(shè)置估算的方法
self.tabelView.estimateRowHeight = 10
-
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
通過代理方法
- 處理不等高 cell 自動(dòng)計(jì)算(iOS8開始才支持)
- 告訴tableView所有cell的真實(shí)高度是自動(dòng)計(jì)算(根據(jù)設(shè)置的約束來計(jì)算)
self.tableView.rowHeight = UITableViewAutomaticDimension;
- 告訴tableView所有cell的估算高度.
self.tableView.estimatedRowHeight = 44;
- 這種需要設(shè)置約束連線尝江,然后在 cell 的 setModel 方法中進(jìn)行判斷
// 設(shè)置配圖數(shù)據(jù) if (status.picture) { // 有配圖 self.pictureHeight.constant = 100; self.pictureBottom.constant = 10; self.pictureImageView.image = [UIImage imageNamed:status.picture]; } else { // 沒有配圖 // 設(shè)置圖片高度為0 self.pictureHeight.constant = 0; // 設(shè)置圖片底部間距為0 self.pictureBottom.constant = 0;
- 告訴tableView所有cell的真實(shí)高度是自動(dòng)計(jì)算(根據(jù)設(shè)置的約束來計(jì)算)
```-
iOS 8之前
- 如果cell內(nèi)部有自動(dòng)換行的label涉波,需要設(shè)置preferredMaxLayoutWidth屬性
- (void)awakeFromNib { // 手動(dòng)設(shè)置文字的最大寬度(目的是:讓label知道自己文字的最大寬度,進(jìn)而能夠計(jì)算出自己的frame) self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20; }
- 設(shè)置tableView的cell估算高度
// 告訴tableView所有cell的估算高度(設(shè)置了估算高度炭序,就可以減少tableView:heightForRowAtIndexPath:方法的調(diào)用次數(shù)) self.tableView.estimatedRowHeight = 200;
- 在代理方法中計(jì)算cell的高度
XMGStatusCell *cell; - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 創(chuàng)建一個(gè)cell(cell的作用:根據(jù)模型數(shù)據(jù)布局所有的子控件啤覆,進(jìn)而計(jì)算出cell的高度) if (!cell) { cell = [tableView dequeueReusableCellWithIdentifier:ID]; } // 設(shè)置模型數(shù)據(jù) cell.status = self.statuses[indexPath.row]; return cell.height; } ``` 4. 在 cell.m中設(shè)置 height 高度
- (CGFloat)height { // 強(qiáng)制布局cell內(nèi)部的所有子控件(label根據(jù)文字多少計(jì)算出自己最真實(shí)的尺寸) [self layoutIfNeeded]; // 計(jì)算cell的高度 if (self.status.picture) { return CGRectGetMaxY(self.pictureImageView.frame) + 10; } else { return CGRectGetMaxY(self.text_label.frame) + 10; } }
- 程序一開始就會(huì)吧 tabelViewl 的所有cell高度算出來寂拆,確定 contengSize 的大小奢米,從而計(jì)算出滾動(dòng)條的長(zhǎng)度。所以這樣的性能不好纠永,所以蘋果推出了