为什么我的表视图单元格在使用reloadRowsAtIndexPaths重新加载时会消失? [英] Why do my table view cells disappear when reloaded using reloadRowsAtIndexPaths?

查看:153
本文介绍了为什么我的表视图单元格在使用reloadRowsAtIndexPaths重新加载时会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个简单的示例项目:

I have a bare-bones sample project here:

http://dl.dropbox.com/u/7834263/ExpandingCells.zip

在这个项目中,UITableView有自定义UITableViewCell。在每个单元格中有3个包含标签的UIView。

In this project, a UITableView has a custom UITableViewCell. In each cell are 3 UIViews containing a label.

目标是在点击时展开单元格,然后在再次点击时折叠它。单元格本身必须更改其高度以显示子视图。插入或删除行是不可接受的。

The goal is to expand the cell when tapped, then collapse it when tapped again. The cell itself must change it’s height to expose the subviews. Inserting or removing rows is unacceptable.

演示项目几乎按预期工作。事实上,在iOS 4.3中,它的工作非常完美。但是,在iOS 5下,当行崩溃时,前一个单元格神奇地消失。

The demo project works almost as expected. In fact, in iOS 4.3 it works perfect. Under iOS 5, however, when the rows collapse, the previous cells magically disappear.

要重新创建问题,请在iOS 5的模拟器或设备中运行项目并点击第一个单元格以展开它。然后再次点击该单元格以折叠它。最后,直接点击它下面的单元格。前一个消失了。

To re-create the problem, run the project in the simulator or device with iOS 5 and tap the first cell to expand it. Then tap the cell again to collapse it. Finally, tap the cell directly underneath it. The previous one disappears.

继续点击该部分中的每个单元格将导致所有单元格消失,直到缺少整个部分。

Continuing the tapping for each cell in the section will cause all the cells to disappear, to where the entire section is missing.

我也尝试使用reloadData而不是当前的设置,但这会破坏动画并且感觉有点像黑客。 reloadRowsAtIndexPaths应该有效,但问题是为什么不呢?

I’ve also tried using reloadData instead of the current setup, but that ruins the animations and feels a bit like a hack anyway. reloadRowsAtIndexPaths should work, but the question is why doesn’t it?

查看下面发生的事情的图像:

See images of what's happening below:

表出现:

Cell扩展:

单元格崩溃:

单元格消失(当点击下面的单元格时):

Cell disappears (when tapping the cell underneath):

不断重复,直到整个部分消失:

Keep repeating until the entire section disappears:

编辑:
覆盖alpha是一个黑客,但有效。这是另一个黑客修复它,但它为什么修复它?

Overriding the alpha is a hack, but works. Here is another 'hack' that fixes it as well but WHY does it fix it?

JVViewController.m第125行:

JVViewController.m line 125:

if( previousIndexPath_ != nil )
{
    if( [previousIndexPath_ compare:indexPath] == NSOrderedSame ) currentCellSameAsPreviousCell = YES;

    JVCell *previousCell = (JVCell*)[self cellForIndexPath:previousIndexPath_];

    BOOL expanded = [previousCell expanded];
    if( expanded )
    {
        [previousCell setExpanded:NO];
        [indicesToReload addObject:[previousIndexPath_ copy]];
    }
    else if( currentCellSameAsPreviousCell )
    {
        [previousCell setExpanded:YES];
        [indicesToReload addObject:[previousIndexPath_ copy]];
    }

    //[indicesToReload addObject:[previousIndexPath_ copy]];
}

编辑2:

对演示项目做了一些小改动,值得检查并查看JVViewController didSelectRowAtIndexPath方法。

Made a few minor changes to demo project, worth checking out and reviewing JVViewController didSelectRowAtIndexPath method.

推荐答案

你的问题出在setExpanded:在JVCell.m中,你是在那个方法中直接编辑目标单元格的框架。

Your problem is in setExpanded: in JVCell.m, you are directly editing the frame of the target cell in that method.

- (void)setExpanded:(BOOL)expanded
{
    expanded_ = expanded;

    CGFloat newHeight = heightCollapsed_;
    if( expanded_ ) newHeight = heightExpanded_;

    CGRect frame = self.frame;
    frame.size.height = newHeight;
    self.frame = frame;
}

更新为:

- (void)setExpanded:(BOOL)expanded
{
    expanded_ = expanded;
}

然后删除对 -reloadRowsAtIndexPaths:withRowAnimation的调用: 在JVViewController.m的第163行,它将按预期动画。

Then remove the call to -reloadRowsAtIndexPaths:withRowAnimation: at line 163 of JVViewController.m and it will animate as expected.

-reloadRowsAtIndexPaths:withRowAnimation:期望为提供的indexPaths返回不同的单元格。由于您只调整尺寸 -beginUpdates & -endUpdates 足以再次布置表格视图单元格。

-reloadRowsAtIndexPaths:withRowAnimation: expects different cells to be returned for the provided indexPaths. Since you are only adjusting sizes -beginUpdates & -endUpdates is sufficient to layout the table view cells again.

这篇关于为什么我的表视图单元格在使用reloadRowsAtIndexPaths重新加载时会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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