ios8.0TableView自动计算cell高度

设置一个缓存高度的字典

@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度

实行懒加载

pragma mark - Getters

  • (NSMutableDictionary *)heightAtIndexPath
    {
    if (!_heightAtIndexPath) {
    _heightAtIndexPath = [NSMutableDictionary dictionary];
    }
    return _heightAtIndexPath;
    }

如果想要移除plain下面多出来的分割线

请在这个方法里执行

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    //如果想去除plain下面多出来的分割线
    //那么就

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    

}

下面方法给一个预算高度

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
if(height)
{
return height.floatValue;
}
else
{
return 100;
}
}

在willdisplay方法里把计算的高度缓存一下

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSNumber *height = @(cell.frame.size.height);
    [self.heightAtIndexPath setObject:height forKey:indexPath];

    /此处做了个简单的动画效果/
    NSArray * array = tableView.indexPathsForVisibleRows;
    NSIndexPath * firstIndexPath = array [0];
    cell.layer.anchorPoint = CGPointMake(0, 0.5);
    cell.layer.position = CGPointMake(cell.layer.position.x, cell.layer.position.y);
    if (firstIndexPath.row < indexPath.row) {
    cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 0.5);
    }else {
    cell.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 0, 1.0);
    }
    cell.alpha = 0;
    [UIView animateWithDuration:1.0 animations:^{
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1.0;
    }];

}

如果要进行数据的刷新 请把heightAtIndexPath清空,重新计算高度

你可能感兴趣的:(ios8.0TableView自动计算cell高度)