【问题标题】:Expand and Collapse tableview Cell展开和折叠 tableview 单元格
【发布时间】:2016-05-11 14:28:21
【问题描述】:

嘿,我是 ios 开发的新手。我想展开和折叠tableview,所以我的代码是:

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

还有我的最小化行方法,比如 as

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

通过这种方法和编码,我实现了Expand and Collapse tableViewCell,这是可行的。因为它在单击时展开 tableViewCell,并在第二次单击可展开单元格时将其折叠。

但我想这样工作:单击任何可展开单元格时,其他可展开单元格将自动折叠。

谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    在调用didSelectRowAtIndexPath: 之后,您可以保存最后一个可展开以折叠的单元格。

    在你的@interface之后:

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

    也许代码需要一些调整,但看看它是否可以帮助你。

    【讨论】:

      【解决方案2】:

      有很多可用于展开和折叠表格的示例。

      你可以参考下面的例子

      http://www.appcoda.com/expandable-table-view/

      【讨论】:

        【解决方案3】:

        您可以使用HVTableView 轻松展开和折叠单元格

        这是 UITableView 的子类,具有展开/折叠功能, 在许多情况下都很有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-26
          • 1970-01-01
          • 2013-07-14
          • 2016-08-25
          • 2016-08-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多