Ios 展开和折叠tableview单元格
嘿,我是ios开发的新手。我想展开和折叠tableview,所以我的代码是:Ios 展开和折叠tableview单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,嘿,我是ios开发的新手。我想展开和折叠tableview,所以我的代码是: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section != 0) { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSDictiona
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section != 0)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar )
{
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:1]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationTop];
}
}
}
我的最小化行方法如下
-(void)miniMizeThisRows:(NSArray*)ar{
for(NSDictionary *dInner in ar ) {
NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:@"Objects"];
if(arInner && [arInner count]>0){
[self miniMizeThisRows:arInner];
}
if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
[self.arForTable removeObjectIdenticalTo:dInner];
[self.tblLeft deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:1]]withRowAnimation:UITableViewRowAnimationBottom];
}
}
}
通过这种方法和编码,我实现了展开和折叠tableViewCell,这很有效。当它在单击时展开tableViewCell,并在第二次单击可展开单元格时将其折叠
但我想这样做:单击任何可扩展单元,其他可扩展单元将自动折叠
有人能帮我吗
提前谢谢 调用
didselectrowatinexpath:
后,可以保存最后一个可展开折叠的单元格
在您的@界面之后
放置:
@property (nonatomic, strong) NSIndexPath *lastIndexPath;
现在,您可以在didSelectRowAtIndexPath
中使用它,如:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Collapse the last cell expanded
if (indexPath.section != 0) {
if(self.lastIndexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
NSDictionary *d=[self.arForTable objectAtIndex:self.lastIndexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
} [self miniMizeThisRows:ar];
}
}
// Save the current indexPath as the last one expanded
self.lastIndexPath = indexPath;
// This is your current code - I didn't change it
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar )
{
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:1]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationTop];
}
}
}
也许代码需要一些调整,但看看它是否能帮助您 调用
didselectrowatinexpath:
后,可以保存最后一个可展开折叠的单元格
在您的@界面之后
放置:
@property (nonatomic, strong) NSIndexPath *lastIndexPath;
现在,您可以在didSelectRowAtIndexPath
中使用它,如:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Collapse the last cell expanded
if (indexPath.section != 0) {
if(self.lastIndexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
NSDictionary *d=[self.arForTable objectAtIndex:self.lastIndexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
} [self miniMizeThisRows:ar];
}
}
// Save the current indexPath as the last one expanded
self.lastIndexPath = indexPath;
// This is your current code - I didn't change it
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar )
{
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:1]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationTop];
}
}
}
也许代码需要一些调整,但看看它是否能帮助您 有许多可用于展开和折叠表的示例 您可以参考下面的示例
有许多可用于展开和折叠表的示例 您可以参考下面的示例
你确实可以通过使用 这是UITableView的一个子类,具有展开/折叠功能 在许多场景中都很有用
通过使用,您确实可以轻松地展开和折叠单元格 这是UITableView的一个子类,具有展开/折叠功能 在许多场景中都很有用