Expand/collapse cell collapse row when other rows selected in ios

纵饮孤独 提交于 2019-12-25 04:11:42

问题


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:


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.



来源:https://stackoverflow.com/questions/18312033/expand-collapse-cell-collapse-row-when-other-rows-selected-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!