【问题标题】:Table Cell expansion laggy表格单元扩展滞后
【发布时间】:2014-04-29 23:26:10
【问题描述】:

我有一个自定义表格视图和单元格,其中一个单元格在选中时会展开。它现在可以正常运行并相应地运行。但是,当我选择单元格来展开它们时,大约需要半秒钟才能做出响应。下面的代码位于 didSelectRowAtIndexPath 中。 我的假设是在 beginUpdates 和 endUpdates 之间,有太多的事情要增加原始单元格的高度,然后更新整个表格视图。还有其他方法可以更好地实现吗?

**[_tableView beginUpdates];**

ReviewTestTableCell *reviewCell1 = (ReviewTestTableCell *)[tableView cellForRowAtIndexPath:indexPath];

        CGRect rect = CGRectMake(reviewCell1.review.width, 900, reviewCell1.review.width, 900);

        CGRect textRectb = [reviewCell1.review textRectForBounds:rect limitedToNumberOfLines:1000];
        float labelHeight = textRectb.size.height;

        reviewCell1.review.height = labelHeight;
        expandHeight = labelHeight + 75 ;

        if ([[userdefaults objectForKey:@"merchantType"] isEqual:@"T"])
        {reviewCell1.height = labelHeight + 50;
        reviewCell1.bottomRatingView.height = reviewCell1.bottomRatingView.height - 20;

        }
        else
        {
            reviewCell1.height = labelHeight + 75;}

        reviewCell1.bottomRatingView.hidden = NO;
        reviewCell1.bottomRatingView.top = reviewCell1.review.bottom;

        **[_tableView endUpdates];**

       [_isExpandList replaceObjectAtIndex:indexPath.row withObject:@1];
    }

编辑/添加:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 UITableViewCell *cell3 = [self tableView:tableView cellForRowAtIndexPath:indexPath];

if ([_isExpandList[indexPath.row]  isEqual: @1] && [[_dataList objectAtIndex:indexPath.row] valueForKey:@"Item1Score"] != nil) {

           return  cell3.height + 3 + 65;
}
else if ([_isExpandList[indexPath.row]  isEqual: @0])
{
return cell3.height +5;
}
else return cell3.height +3;
}

【问题讨论】:

    标签: ios objective-c tableview tablecell


    【解决方案1】:

    您不应该经历那种试图手动扩展单元格的精心设计。你当然不应该手动调用willDisplayCell

    使用this question 的答案中描述的方法,您应该有一个属性或其他东西来跟踪选择了哪个单元格并让您的heightForRowAtIndexPath: 方法针对特定的 indexPath 进行调整,然后调用

    [tableView beginUpdates];
    [tableView endUpdates];
    

    将为每个单元格调用heightForRowAtIndexPath,当它与所选行匹配时,您的方法将为其提供更大的高度。 tableView 将平滑调整单元格的高度。

    类似于:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([self.selectedIndexPath isEqual:indexPath]) {
            return 80.0f;
        }
    
        return tableView.rowHeight;
    }
    

    【讨论】:

    • 感谢大家的帮助,我已经删除了willDisplayCell并更新了HeightForRowIndexPath,但还是不流畅。我在 heightForRowIndexPath 中的 if 语句有问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2017-08-02
    相关资源
    最近更新 更多