展开和折叠tableview单元格

时间:2016-05-11 14:28:22

标签: ios objective-c uitableview

嘿,我是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];
        }
    }
}

和我的最小化行方法如

-(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并在第二次单击Expandable cell时折叠它。

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

任何人都能帮助我吗?

提前致谢。

3 个答案:

答案 0 :(得分:0)

您可以在调用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];
        }
    }
}

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

答案 1 :(得分:0)

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

您可以参考以下示例

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

答案 2 :(得分:0)

使用HVTableView

,您可以轻松扩展和折叠单元格
  

这是具有展开/折叠功能的UITableView的子类   在许多场景中都很有用。