扩展表格视图单元格消失 [英] Expanding table view cells disappearing

查看:100
本文介绍了扩展表格视图单元格消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过setExpanded:方法调用更改高度来扩展单元格。

I have cells that expand by changing their height with a setExpanded: method call.

然后调用reloadRowsAtIndexPaths:刷新单元格。

I then call reloadRowsAtIndexPaths: to refresh the cells.

问题是细胞消失并随机重新出现。我怀疑索引的工作方式是否正确。

The problem is the cells simply disappear and randomly re-appear. I suspect this has to due with the way the indexing is working.

如果我调用reloadData或beginUpdates / endUpdates,单元格会按预期工作,但我会丢失动画。

If I call reloadData or beginUpdates/endUpdates the cells work as expected, but I lose the animations.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JVCell *cell = (JVCell*)[tableView cellForRowAtIndexPath:indexPath];
    JVCell *previousCell = nil;

    if( previousIndexPath_ != nil ) // set to nil in viewDidLoad
    {
        previousCell = (JVCell*)[tableView cellForRowAtIndexPath:previousIndexPath_];
    }


    // expand or collapse cell if it's the same one as last time
    if( previousCell != nil && [previousIndexPath_ compare:indexPath] == NSOrderedSame && [previousCell expandable] )
    {
        [previousCell setExpanded:![cell expanded]];
        NSArray *indexPathArray = [NSArray arrayWithObject:previousIndexPath_];
        [tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        // collapse previous cell
        if( previousCell != nil && [previousCell expandable] )
        {
            if( [previousCell expanded] ) [previousCell setExpanded:NO];
            NSArray *indexPathArray = [NSArray arrayWithObject:previousIndexPath_];
            [tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
        }

        // expand new cell
        if( [cell expandable] )
        {
            [cell setExpanded:YES];
            NSArray *indexPathArray = [NSArray arrayWithObject:indexPath];
            [tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }

    previousIndexPath_ = indexPath;

    // works as expected, but I lose the animations
    //[tableView reloadData];

    // works as expected, but I lose the animations
    //[tableView beginUpdates];
    //[tableView endUpdates];
}

编辑:已更新,包括cellForRowAtIndexPath和heightForRowAtIndexPath方法:

updated to include cellForRowAtIndexPath and heightForRowAtIndexPath methods:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    JVCellSectionData *sectionData = [sections_ objectAtIndex:section]; // NSArray of SectionData objects
    NSArray *cellArray = [sectionData cells]; // NSArray of cells
    UITableViewCell *cell = [cellArray objectAtIndex:row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    JVCellSectionData *sectionData = [sections_ objectAtIndex:section];
    NSArray *cellArray = [sectionData cells];
    JVCell *cell = [cellArray objectAtIndex:row];
    return [cell cellHeight]; // changed when selected with setExpanded: method
}

编辑2:我制作了Quicktime视频,了解正在发生的事情并提取屏幕截图。

Edit 2: I made a Quicktime video of what was happening and extracted screen shots.

我正在尝试做的是扩展单元格,而不是替换,插入或删除细胞。每个单元格都有一个或多个子视图。细胞的高度根据它是否扩展而改变。内容视图添加了子视图,它的clipToBounds属性为YES。当单元格展开或折叠时,高度值随单元格的框架一起变化(包括背景视图和选定的背景视图)。我在扩展之前,期间和之后记录了所有帧值,并且它们都与它们的状态和位置一致。

What I'm attempting to do is expand a cell, not replace, insert or delete cells. Each cell has one or more subviews. The height of the cell changes depending on whether it's 'expanded' or not. The content view has the subviews added to it, and it's clipToBounds property is YES. When the cells expands or collapses the height value changes along with the frame of the cell (including background view and selected background view). I've logged all the frame values before, during and after expansion, and they are all consistent with their state and position.

请记住,这在iOS 4.3上正常运行,如下所示:

Keep in mind that this works normally on iOS 4.3, as shown below:

推荐答案

我认为iOS之间的行为没有任何区别模拟器中的5.0和4.3.2,或者我的p上的5.0磨练 - 每个细胞以相同的方式消失。 (我在我的旧iPod touch上测试4.2.1但是Xcode 4不能测试.Grr ..)看起来有一个简单的解决方法:如果你同时执行reloadRowsAtIndexPaths:withRowAnimation:扩展/折叠你的细胞后然后是reloadData调用,它似乎保留了动画。

I don't see any difference in behavior between iOS 5.0 and 4.3.2 in the simulator, or 5.0 on my phone—the cells disappear in the same way on each. (I'd test on 4.2.1 on my older iPod touch but Xcode 4 can't. Grr..) It looks like there's an easy workaround, though: If you do both reloadRowsAtIndexPaths:withRowAnimation: after expanding/collapsing your cells and then the reloadData call after that, it appears to preserve the animation.

这是一个小型的演示项目。很难说实际问题是什么 - 这只是UIKit如何进行细胞动画的一些奇怪的副作用。如果您继承[UITableViewCell setAlpha:],则可以看到表视图将单元格的alpha设置为0并将其保留在那里。很奇怪。

Here's a small demo project for this. It's hard to say what the actual problem was—it's just some odd side effect of how UIKit is doing the cell animation. If you subclass [UITableViewCell setAlpha:] you can see that the table view is setting the cell's alpha to 0 and leaving it there. Weird.

编辑:嗯,这很奇怪。它没有起作用(正在做旧的行为,细胞消失了),但现在又恢复正常了。也许我的版本运行错误。无论如何,请告诉我这是否适合您。

Well that's weird. It wasn't working for a bit (was doing the old behavior, with cells disappearing), but now it's right again. Maybe I was running the wrong version. Anyway, let me know if this works for you.

这篇关于扩展表格视图单元格消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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