展开和折叠 tableview 单元格 [英] Expand and Collapse tableview Cell

查看:34
本文介绍了展开和折叠 tableview 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey i am newbie in ios development. I want to expand and collapse tableview, so my code for doing it is:

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

和我的最小化行方法,如

and my minimize row method like 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 并在第二次单击可扩展单元格时折叠它.

From this method and coding I achieved the Expand and Collapse tableViewCell, this works. As It expands tableViewCell on click and collapses it when second time click on Expandable cell.

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

But I want to work as such: On clicking of any expandable cell, other expandable cell will be collapse automatically.

有人可以帮我吗?

提前致谢.

推荐答案

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

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

This is a subclass of UITableView with expand/collapse feature that comes useful in many scenarios.

这篇关于展开和折叠 tableview 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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