tableView點(diǎn)擊cell之后不會(huì)一直有背景效果
- [tableView deselectRowAtIndexPath:indexPath animated:NO];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;(始終沒(méi)有效果)
- 刷新特定行的cell
[self.tableView reloadRowsAtIndexPaths:@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
]
withRowAnimation:UITableViewRowAnimationLeft];
- 插入特定行數(shù)的cell
[self.tableView insertRowsAtIndexPaths:@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
]
withRowAnimation:UITableViewRowAnimationLeft];
- 刪除特定行數(shù)的cell
[self.tableView deleteRowsAtIndexPaths:@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
]
withRowAnimation:UITableViewRowAnimationLeft];
非等高的cell(方法一: 用model保存cell的高度)
- 在模型中增加一個(gè)cellHeight屬性,用來(lái)存放對(duì)應(yīng)cell的高度
- 在cell的模型屬性set方法中調(diào)用[self layoutIfNeed]方法強(qiáng)制布局,然后計(jì)算出模型的cellheight屬性值
- 在控制器中實(shí)現(xiàn)tableView:estimatedHeightForRowAtIndexPath:方法,返回一個(gè)估計(jì)高度,比如200
- 在控制器中實(shí)現(xiàn)tableView:heightForRowAtIndexPath:方法,返回cell的真實(shí)高度(模型中的cellHeight屬性)
- 在?Model的.h文件中
/** cell的高度 */
@property (assign, nonatomic) CGFloat cellHeight;
- 在自定義的cell的.h文件中聲明Model
/** 模型數(shù)據(jù) */
@property (nonatomic, strong) NBStatus *status;
- 在自定義的cell的.m文件中,模型屬性的set方法中
- (void)setStatus:(NBStatus *)status
{
_status = status;
//各種賦值操作.........
// 強(qiáng)制布局
[self layoutIfNeeded];
// 計(jì)算cell的高度
status.cellHeight = ... + 10;
}
- 在控制器的.m文件中
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NBStatusCell *cell = [NBStatusCell cellWithTableView:tableView];
cell.status = self.statuses[indexPath.row];
return cell;
}
/**返回每一行的高度*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NBStatus *staus = self.statuses[indexPath.row];
return staus.cellHeight;
}
/**
* 返回每一行的估計(jì)高度
* 只要返回了估計(jì)高度,那么就會(huì)先調(diào)用tableView:cellForRowAtIndexPath:方法創(chuàng)建cell,再調(diào)用tableView:heightForRowAtIndexPath:方法獲取cell的真實(shí)高度*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
非等高的cell(方法二: 用字典保存cell的高度)
/** 存放所有cell的高度 */
@property (strong, nonatomic) NSMutableDictionary *heights;
/////////////////
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NBStatusCell *cell = [NBStatusCell cellWithTableView:tableView];
cell.status = self.statuses[indexPath.row];
// 強(qiáng)制布局
[cell layoutIfNeeded];
// 存儲(chǔ)高度
self.heights[@(indexPath.row)] = @(cell.height);
return cell;
}
#pragma mark - 代理方法
/**
* 返回每一行的高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self.heights[@(indexPath.row)] doubleValue];
}
/**
* 返回每一行的估計(jì)高度
* 只要返回了估計(jì)高度,那么就會(huì)先調(diào)用tableView:cellForRowAtIndexPath:方法創(chuàng)建cell,再調(diào)用tableView:heightForRowAtIndexPath:方法獲取cell的真實(shí)高度
*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
當(dāng)然也可以用用一個(gè)FrameModel來(lái)保存高度,基本原理都差不多,本文主要是注意那個(gè)估計(jì)高度,因?yàn)閷?shí)現(xiàn)那個(gè)方法只會(huì),tableView的代理方法的順序會(huì)發(fā)生變化