UITableView 以估计的行高度反弹 [英] UITableView bounces with estimatedRowHeight

查看:20
本文介绍了UITableView 以估计的行高度反弹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xib 中为我的 customCell 使用 autoLayout,因为我想根据文本为行设置可变高度.

I am using autoLayout for my customCell in xib, because I want to have variable height for rows based on the text.

现在在我的 VC.m

我正在使用这些代码行

- (void)viewdidLoad
{
    m_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];       
    m_tableView.estimatedRowHeight = 97.0;
   m_tableView.rowHeight = UITableViewAutomaticDimension;
}

这是我的 cellForRow 函数

here is my cellForRow function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *tableCell = (NotesCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (tableCell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        tableCell = [nib objectAtIndex:0];
    }    
    // Configure the cell...


     //This code is used to make the separator full width. Also the tableview separator insets should be set to zero

    tableCell.layoutMargins = UIEdgeInsetsZero;
    tableCell.preservesSuperviewLayoutMargins = NO;

    return tableCell;
}

所以现在每当我更新一行时,我都会调用这些代码行

So now whenever I update a row I call these lines of code

[m_tableView beginUpdates];
    [m_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:m_indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [m_tableView endUpdates];

当我这样做时,Tableview 会反弹.我不知道出了什么问题

When I do this, by Tableview just bounces. I dont know what is going wrong

问候兰吉特

推荐答案

发生这种情况是因为当您重新加载单元格时,表格视图再次询问所有单元格的高度.任何位于滚动位置上方的屏幕外的单元格都将被要求提供它们的估计行高,如果它们与当前高度不同,则会导致单元格更改高度,您将看到表格像那样弹跳.

This is happening because when you reload your cell the table view is asking for the heights of all the cells again. Any cells that are offscreen above your scroll position will be asked for their estimated row heights and if they are different from their current height it will cause the cells to change height and you will see the table bounce like that.

计算完单元格的高度后,您应该将该值缓存在某处,然后实现 tableView:estimatedHeightForRowAtIndexPath: 并返回单元格的实际高度(如果它们有一个而不是您的估计高度).

Once your cells have their heights calculated you should cache that value somewhere and then implement tableView:estimatedHeightForRowAtIndexPath: and return the cells actual height if they have one instead of your estimated height.

这篇关于UITableView 以估计的行高度反弹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆