ios8.0TableView自动计算cell高度

设置一个缓存高度的字典

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

实行懒加载

<pre>

pragma mark - Getters

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

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

请在这个方法里执行
<pre>

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

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

}
</pre>

下面方法给一个预算高度

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

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

<pre>

  • (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;
    }];

}
</pre>

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容