首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >展开表视图单元格消失

展开表视图单元格消失
EN

Stack Overflow用户
提问于 2012-02-22 13:46:50
回答 7查看 6.2K关注 0票数 17

我的单元格通过使用setExpanded: method调用改变它们的高度来扩展。

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

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

如果我调用reloadData或开始更新/结束更新,单元格按预期工作,但我失去动画。

代码语言:javascript
运行
复制
- (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方法:

代码语言:javascript
运行
复制
- (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:,我制作了一段关于正在发生的事情的快速视频并提取了屏幕截图。

我要做的是展开一个单元格,而不是替换、插入或删除单元格。每个单元格都有一个或多个子视图。单元格的高度取决于它是否“展开”。content视图中添加了子视图,并且它的clipToBounds属性是肯定的。当单元格展开或折叠时,高度值随单元格的帧(包括背景视图和选定的背景视图)而变化。在展开之前、期间和之后,我记录了所有的帧值,它们都与它们的状态和位置一致。

请记住,这通常适用于iOS 4.3,如下所示:

EN

Stack Overflow用户

发布于 2012-03-01 12:49:59

@Javy,

谢谢你的问题。我正在开发具有类似行为的表:我的表视图单元格(UITextView)中有以下文本视图(UITableViewCell):

如果需要新行显示文本而不滚动,则展开

  1. ;如果删除新行,则
  2. 折叠。

所以我发现了同样的问题。在iOS 5.x中,当我开始键入文本时,它突然变得不可见了。但是在iOS 4.x中,一切都很好。

我已经找到了解决办法,而且对我来说效果很好。

解决方案:只是在重新加载特定单元时尝试用UITableViewRowAnimationNone替换动画类型的UITableViewRowAnimationAutomatic

一些附加代码:在一分钟内重新加载两个单元格:

代码语言:javascript
运行
复制
    NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:2];
    // collapse previous cell
    if( previousCell != nil && [previousCell expandable] )
    {
        if( [previousCell expanded] ) [previousCell setExpanded:NO];
        [indexes addObject:previousIndexPath_];
    }

    // expand new cell
    if( [cell expandable] )
    {
        [cell setExpanded:YES];
        [indexes addObject:indexPath];
    }

    [tableView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone];

希望它能帮到你。

票数 2
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9396055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档