美文网首页
ios8.0TableView自动计算cell高度

ios8.0TableView自动计算cell高度

作者: Supersweet007 | 来源:发表于2016-10-27 11:10 被阅读95次

设置一个缓存高度的字典

<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清空,重新计算高度

相关文章

网友评论

      本文标题:ios8.0TableView自动计算cell高度

      本文链接:https://www.haomeiwen.com/subject/qvwjuttx.html