1、tableView的邊距
2、tableView的常用方法
3、cell的重用機(jī)制
4、 自定義cell的方法
5、 計(jì)算不同cell的高度
TableView應(yīng)該是非常常用的控件之一,要熟練運(yùn)用!下面是一些開(kāi)發(fā)過(guò)程中的小積累。 如果一個(gè)界面需要用到兩個(gè)tableView,那么這兩個(gè)表視圖的數(shù)據(jù)源和代理可以給一個(gè)控制器, 用的時(shí)候,只要判斷就好了
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) { // 左邊的類(lèi)別表格
return self.types.count;
}else { // 右邊的用戶(hù)表格
return self.users.count;
}
}
- 這個(gè)屬性很好用, 可以調(diào)整tableview的邊距(顯示位置)
// 設(shè)置inset
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
self.userTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
- tableView的常用方法和屬性
一、 兩個(gè)必須實(shí)現(xiàn)的方法, 必須實(shí)現(xiàn)數(shù)據(jù)源協(xié)議喔<UITableViewDataSource>
/** 每一組顯示多少個(gè)cell, 沒(méi)有分組當(dāng)然就默認(rèn)一組咯 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
/** 創(chuàng)建cell的方法 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
二、 選中cell調(diào)用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- cell 的常用方法和屬性
一、 說(shuō)道cell那么就說(shuō)下cell的重用
// 因?yàn)槊炕瑒?dòng)一次,這里都要調(diào)用一次,所有用static就只會(huì)調(diào)用一次了,作為標(biāo)記
static NSString *ID = @"status";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
// 如果閑置池里面沒(méi)有就自己創(chuàng)建
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
return cell;
二、很多時(shí)候我們需要自定義cell來(lái)滿(mǎn)足自己的需求, 可以在storyboard中自定義也可以在xib中自定義
- storyboard中給cell一個(gè)標(biāo)識(shí), 用到cell的時(shí)候直接去緩沖池里面找
static NSString *ID = @"deal";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
return cell;
- xib自定義cell, 也需要給cell一個(gè)標(biāo)識(shí),用的時(shí)候先注冊(cè),再使用
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BSRecommendTypeTableViewCell class]) bundle:nil] forCellReuseIdentifier:typeCell];
// 在緩存池中讀取
BSRecommendTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:typeCell];
return cell;
- 加載xib的時(shí)候我們可以重寫(xiě)這個(gè)方法監(jiān)聽(tīng)選中或取消選中
// 重寫(xiě)selected方法, 可以監(jiān)聽(tīng)cell的選中和取消選中
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
/**
* 這個(gè)指示器什么時(shí)候隱藏呢,沒(méi)有點(diǎn)擊的時(shí)候隱藏
*/
self.seletedIndicator.hidden = !selected;
self.textLabel.textColor = selected ? BSColor(219, 21, 26) : BSColor(78, 78, 78);
}
- 加載xib會(huì)來(lái)這個(gè)方法, 可以在這里做些事情
// 創(chuàng)建xib的時(shí)候調(diào)用該方法
- (void)awakeFromNib {
// 背景顏色
self.backgroundColor = BSColor(244, 244, 244);
self.seletedIndicator.backgroundColor = BSColor(219, 21, 26);
}
- 重新布局子控件, 復(fù)寫(xiě)layoutSubviews
// 布局子控件
- (void)layoutSubviews {
[super layoutSubviews];
self.textLabel.y = 2;
self.textLabel.height = self.contentView.height - 2 *self.textLabel.y;
}
我們有時(shí)候需要這樣的cell,很明顯是有間隙的
Snip20160513_1.png
怎么做呢!有很多方法,這里我覺(jué)得最簡(jiǎn)單的便是攔截frame,灰色的就是一個(gè)背景圖片,如果高度變成 1 不就是分割線了么!
// 重寫(xiě)frame攔截尺寸
- (void)setFrame:(CGRect)frame {
// 左右的空隙
frame.origin.x = 10;
frame.size.width -= 2 * frame.origin.x;
// 不用添加任何控件,實(shí)現(xiàn)分割線
frame.size.height -= 10;
// 調(diào)用父類(lèi)要寫(xiě)在后面
[super setFrame:frame];
}
計(jì)算不同cell的高度, cell中的內(nèi)容是豐富多彩的,那么計(jì)算高度,要知道每個(gè)內(nèi)容的不同高度來(lái)決定cell的高度。下面是根據(jù)文字的高度,得到cell的高度
// 文字的最大尺寸
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * BSTopicCellMargin, MAXFLOAT);
CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} context:nil].size.height;