gpt4 book ai didi

ios - 从上一个选定部分的 UITableView 中删除行

转载 作者:行者123 更新时间:2023-11-29 12:21:37 25 4
gpt4 key购买 nike

目前我有一个 UITableView,它使用当前代码展开和折叠。

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section{
if (section>=0) return YES;
return NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger numberofRows = 0;

numberofRows = nameArray.count;

if (numberofRows != 0){
self.mainTableView.hidden = false;
}

return numberofRows;
}

- (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];
}

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];

cell.accessoryView = [ALCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:ALCustomColoredAccessoryTypeDown];

}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [ALCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:ALCustomColoredAccessoryTypeUp];

}
}
else {
NSLog(@"Selected Section is %ld and subrow is %ld ",(long)indexPath.section ,(long)indexPath.row);

}

}
}

This works really well and upon selection of a UITableViews section the rows are expanded and populated with the correct data and when the same section is selected the rows are removed and appear collapsed.

但是我想做的是以某种方式自动折叠之前选择的部分并在用户选择新的 indexpath.section 时删除之前部分中的行。

我曾尝试将选定的部分索引路径存储到一个数组中,并根据此值删除行,但我认为我的做法是错误的,因为我遇到了断言失败。

所以我的问题如下:-

如何在选择另一个部分时从 uitableviews 部分自动折叠(删除行)

提前感谢您的帮助

托马斯

最佳答案

我在为我的应用程序编写代码时遇到过这个问题,我创建了这篇没有任何评论或回答的帖子......当我单击其他部分并展开该部分的行时,您可以使用我为我编写的这段代码来折叠先前展开的部分行,这段代码对我有用,试试吧...
`

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
if (section>=0) return YES;

return NO;
}




static NSInteger Value=0;

static NSInteger Value2=0;
static CFIndex indexPre=0;
static NSMutableArray *preArray;
static NSIndexPath *path;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
TableViewCell *cell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.hideButton.hidden=NO;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger index=[expandedSections firstIndex];

if ([expandedSections indexGreaterThanIndex:index])
{
NSLog(@"%lu hhhwww", index);
}

NSInteger section = indexPath.section;

static NSInteger rows;
static NSInteger PreRows;
NSLog(@"%lu MY INDEXES1",(unsigned long)[expandedSections count]);
NSMutableArray *tmpArray = [NSMutableArray array];
preArray = [NSMutableArray array];

BOOL currentlyExpanded = [expandedSections containsIndex:section];


if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
Value=0;
Value2=0;
cell.hideButton.hidden=YES;
}
else
{
if (Value==0)
{

[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
PreRows = [self tableView:tableView numberOfRowsInSection:section];
indexPre=indexPath.section;
Value=1;

}
else if (Value==1)
{
Value2=1;
NSLog(@"indexPre == %ld %@ my indexxxxx", indexPre, expandedSections);
[expandedSections removeIndex:indexPre];
NSLog(@"indexPre == %ld %@ my indexxxxx", indexPre, expandedSections);
[self.tableView reloadData];
[expandedSections addIndex:section];
NSLog(@"indexPre == %ld %@ my indexxxxx", indexPre, expandedSections);
rows=[self tableView:tableView numberOfRowsInSection:section];
}


}

NSLog(@"%@ MY INDEXES",expandedSections);
if (Value2==1)
{
for (int i=1; i<PreRows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:indexPre];
[preArray addObject:tmpIndexPath];
}
NSLog(@"my arrray %@", preArray);
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
NSLog(@"my arrray tmparray%@", preArray);

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



if (currentlyExpanded )
{


[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:NO];

cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];


}
else if(Value==1)
{
if (Value2==1)
{

[tableView beginUpdates];

[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:NO];


cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

[tableView endUpdates];
[tableView beginUpdates];

[tableView endUpdates];

indexPre=indexPath.section;
}
else
{

[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}


}

}

}
}

`

关于ios - 从上一个选定部分的 UITableView 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com