ios - 如何通过代码关闭 uitableview 中打开的单元格 (Objective-C)

标签 ios objective-c uitableview

我正在向我的应用程序添加一个过滤器功能,但每次应用过滤器时,当我更改过滤器时,以前打开的单元格不会关闭,有没有办法实现这个?

我目前正在使用:

[self.tableview reloadData];

每次应用过滤器时重新加载表格

打开和关闭:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    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 = [expandedSections containsIndex:section];
            NSInteger rows;

            NSMutableArray *tmpArray = [NSMutableArray array];


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

            }
            else
            {

                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
                NSLog(@"Section:%d", rows);
            }

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

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (currentlyExpanded)
            {
                dispatch_async(dispatch_get_main_queue(), ^{

                [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                });
            }
            else
            {
                  dispatch_async(dispatch_get_main_queue(), ^{
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                  });

            }
        }
}

最佳答案

在 tableview reloadData 之前清除 expandedSections 数组。

[expandedSections removeAllIndexes];
[self.tableView reloadData];

expandedSections 数组维护展开部分详细信息的状态。因此它不会折叠 View 。所以在重新加载 TableView 之前清除数组。

关于ios - 如何通过代码关闭 uitableview 中打开的单元格 (Objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908063/

相关文章:

ios - 循环结束后如何调用函数?

ios - 如何将 APN 认证上传到 Firebase 以进行 FCM

ios - 如何在 iOS 应用程序中配置 XMPP Facebook 聊天

objective-c - 创建 OS X 搜索过滤器面板

ios - 为什么当我为我的 CABasicAnimation 设置一个较低的持续时间值时它会跳转?

ios - iOS 静态和动态表格 View 单元格的混合

iphone - 如何再次请求访问 iphone 通讯录地址簿的权限?

ios - 如果用户从 UIAlertView 选择取消按钮,则停止从 UITable 中删除用户

objective-c - 我应该使用什么数字对象,需要从 NSString 转换 1.4 并执行 If 小于?

ios - 如何在表格 View 中隐藏行。隐藏索引后应该不会改变 iOS