tableView小結

weixin_34413357發表於2016-05-11
tableView點選cell之後不會一直有背景效果
- [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
- cell.selectionStyle = UITableViewCellSelectionStyleNone;(始終沒有效果)
  • 重新整理特定行的cell
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 插入特定行數的cell
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 刪除特定行數的cell
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];

非等高的cell(方法一: 用model儲存cell的高度)

 - 在模型中增加一個cellHeight屬性,用來存放對應cell的高度
 - 在cell的模型屬性set方法中呼叫[self layoutIfNeed]方法強制佈局,然後計算出模型的cellheight屬性值
 - 在控制器中實現tableView:estimatedHeightForRowAtIndexPath:方法,返回一個估計高度,比如200
 - 在控制器中實現tableView:heightForRowAtIndexPath:方法,返回cell的真實高度(模型中的cellHeight屬性)
  • 在�Model的.h檔案中
/** cell的高度 */
@property (assign, nonatomic) CGFloat cellHeight;
  • 在自定義的cell的.h檔案中宣告Model
/** 模型資料 */
@property (nonatomic, strong) NBStatus *status;
  • 在自定義的cell的.m檔案中,模型屬性的set方法中
- (void)setStatus:(NBStatus *)status
{
    _status = status;
    
   //各種賦值操作.........
    
    // 強制佈局
    [self layoutIfNeeded];
    
    // 計算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;
}
/**
* 返回每一行的估計高度
 * 只要返回了估計高度,那麼就會先呼叫tableView:cellForRowAtIndexPath:方法建立cell,再呼叫tableView:heightForRowAtIndexPath:方法獲取cell的真實高度*/
- (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];
    // 強制佈局
    [cell layoutIfNeeded];
    // 儲存高度
    self.heights[@(indexPath.row)] = @(cell.height);
    return cell;
}

#pragma mark - 代理方法
/**
 *  返回每一行的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.heights[@(indexPath.row)] doubleValue];
}

/**
 * 返回每一行的估計高度
 * 只要返回了估計高度,那麼就會先呼叫tableView:cellForRowAtIndexPath:方法建立cell,再呼叫tableView:heightForRowAtIndexPath:方法獲取cell的真實高度
 */
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

當然也可以用用一個FrameModel來儲存高度,基本原理都差不多,本文主要是注意那個估計高度,因為實現那個方法只會,tableView的代理方法的順序會發生變化