在iOS中选择其他行时展开/折叠单元格折叠行 [英] Expand/collapse cell collapse row when other rows selected in ios

查看:297
本文介绍了在iOS中选择其他行时展开/折叠单元格折叠行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个git hub教程中的plist值来扩展和折叠表格视图。在该教程中,当我按一行时,它正在扩展,当我按同一行时,它再次崩溃了。但是我想要的是当我按一行期望其他打开的行应该折叠时,所以任何人都可以给我一些想法。

I am expanding and collapsing table view using plist value from one git hub tutorial. In that tutorial when i press row it is expanding, when i press the same row it is again collapsing no problem in that. But what i want is when i press a row expect that row other opened rows should be collapsed, so could any one give me some idea.

这是扩展和折叠的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
    NSLog(@"%@",d);
    if([d valueForKey:@"Objects"]) {
        NSArray *ar=[d valueForKey:@"Objects"];
        NSLog(@"%@",ar);


        BOOL isAlreadyInserted=NO;

        for(NSDictionary *dInner in ar ){
            NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
            NSLog(@"%ld",(long)index);


            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:0]];
                [self.arForTable insertObject:dInner atIndex:count++];
            }




            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

-(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.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                                                [NSIndexPath indexPathForRow:indexToRemove inSection:1]
                                                ]
                              withRowAnimation:UITableViewRowAnimationRight];
        }
    }
}


推荐答案

这意味着您希望一次只扩展一个单元格。因此,您应该保存最后一个扩展行的索引路径,并应为该索引路径调用方法,同时应为期望扩展的行插入单元格。

That means you want to expand exactly one cell at a time. So you should save the index path of last expanded row and you should call your method for that index path and at the same time you should insert cell for row which you are expecting to expand.

更新
使成员变量为.h

UPDATED Make member variable in .h

NSIndexPath * lastIndexPath;

NSIndexPath *lastIndexPath;

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];


            if(lastIndexPath.row == indexPath.row)
            {
          NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
        if([d valueForKey:@"Objects"]) {
            NSArray *ar=[d valueForKey:@"Objects"];
              }
          [self miniMizeThisRows:ar];
            }

            else
            {
                 if(lastIndexPath.row)
               {
                    NSDictionary *d=[self.arForTable objectAtIndex:lastIndexPath.row];
                   if([d valueForKey:@"Objects"]) {
                   NSArray *ar=[d valueForKey:@"Objects"];
                   [self miniMizeThisRows:ar];
                   }
               }
                NSUInteger count=indexPath.row+1;
                NSMutableArray *arCells=[NSMutableArray array];
                for(NSDictionary *dInner in ar ) {
                    [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                    [self.arForTable insertObject:dInner atIndex:count++];
                    lastIndexPath = indexPath;

                [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
    }

如果有任何括号,请原谅错过了。请通知我。

Please excuse if any bracket is missed. Please notify me regarding that.

这篇关于在iOS中选择其他行时展开/折叠单元格折叠行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆