关闭UITable的所有部分

时间:2012-11-20 18:47:08

标签: xcode uitableview

我有一个具有可扩展单元格的UItable。 当用户点击某个部分时,它会展开并显示行。但是我需要在新部分打开之前关闭任何先前打开的部分。我想我需要在didselectrow做这个,只是不知道怎么做??

我的didselectrow代码在

之下
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{
    if(tableView.tag == 4)
    {
        //NSLog(@"Did Select Row");

        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            if (!indexPath.row)
            {
                // only first row toggles exapand/collapse
                [tableView deselectRowAtIndexPath:indexPath animated:YES];

                NSInteger section = indexPath.section;
                BOOL currentlyExpanded = [expandedSections3 containsIndex:section];
                NSInteger rows;


                NSMutableArray *tmpArray = [NSMutableArray array];

                if (currentlyExpanded)
                {
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                    [expandedSections3 removeIndex:section];

                }
                else
                {
                    [expandedSections3 addIndex:section];
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                }


                for (int i=1; i<rows; i++)
                {
                    NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                               inSection:section];
                     [tmpArray addObject:tmpIndexPath];
                }

                //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

                if (currentlyExpanded)
                {
                    [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];

                }
                else
                {
                    [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                }
            }

            else {

            }
        }

        //NSLog(@"Button Pressed?");
    }
}

1 个答案:

答案 0 :(得分:0)

尝试从reloadSections:withRowAnimation:内调用UITableView的tableView:didSelectRowAtIndexPath:。这使表视图可以为您指定的部分重新查询您的委托。作为一个好处,您将获得以这种方式添加和删除的行的漂亮动画。

当然,这与您直接操作表视图行的当前解决方案完全不同,因此如果您想尝试reloadSections:withRowAnimation:,则可能需要重写代理的大部分内容。更改将涉及存储节的折叠/展开状态,直接在委托中或委托引用的模型对象中,以便委托在表视图重新查询时具有更新的返回值。 / p>

相关问题