扩展表格视图单元格消失

Tig*_*ing 17 iphone uitableview ios

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

然后我调用reloadRowsAtIndexPaths:刷新单元格.

问题是细胞消失并随机重新出现.我怀疑这与索引工作方式有关.

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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

- (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
}
Run Code Online (Sandbox Code Playgroud)

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

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

分段表视图设计

细胞拍打

细胞扩张

细胞消失了

细胞缩回

细胞再次可见

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

comaprison ios 4到ios 5

dav*_*den 7

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

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

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


小智 5

我遇到过同样的问题.我确认重新加载行时不使用动画可以正常工作.

但事实证明,问题是由于在实际需要新的时候在cellForRowAtIndexPath中返回缓存的UITableViewCell引起的.

reloadRowsAtIndexPaths:withRowAnimation的文档说:"重新加载一行会导致表视图向其数据源询问该行的新单元格."

您的cellForRowAtIndexPath方法返回本地缓存的单元格.返回一个全新的单元格修复了我的问题,并且不需要解决方法......