Expand/collapse cell collapse row when other rows

2019-08-28 20:10发布

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.

This is the code for expanding and collapsing

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

1条回答
Emotional °昔
2楼-- · 2019-08-28 20:37

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.

UPDATED Make member variable in .h

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.

查看更多
登录 后发表回答