我正在向我的应用程序添加一个过滤器功能,但每次应用过滤器时,当我更改过滤器时,以前打开的单元格不会关闭,有没有办法实现这个?
我目前正在使用:
[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/