Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何创建可展开的表格视图单元格??当我点击一个单元格时,它应该展开并显示它包含的子单元格_Ios_Objective C - Fatal编程技术网

Ios 如何创建可展开的表格视图单元格??当我点击一个单元格时,它应该展开并显示它包含的子单元格

Ios 如何创建可展开的表格视图单元格??当我点击一个单元格时,它应该展开并显示它包含的子单元格,ios,objective-c,Ios,Objective C,此外,展开的单元格应转到didselect上的viewcontroller 所有节的行中的数据都是静态的。 我如何在目标c中实现这一点??我尝试搜索该问题,但找不到任何问题。使用以下代码将可展开单元格放入UITableView: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdent

此外,展开的单元格应转到didselect上的viewcontroller 所有节的行中的数据都是静态的。
我如何在目标c中实现这一点??我尝试搜索该问题,但找不到任何问题。

使用以下代码将可展开单元格放入UITableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
    [cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];    
    return cell;
}
用于展开和折叠行的代码位于TableView的DidSelectRow方法中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    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: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:0]]
                      withRowAnimation:UITableViewRowAnimationRight];
        }
    }
}

首先,我为每个静态单元创建了属性,并在viewDidLoad中将它们设置为隐藏:

然后在DidSelectRowatinex中:


我不知道它有多好。。如果有任何错误,请告诉我,因为我对objective c非常陌生。

尝试查找嵌套的tableview。你到底需要哪方面的帮助?(如果你正在找人为你编写所有代码的人,这可能是错误的网站。)@MehulSojitra:谢谢,我试过使用,但我不想使用任何库。不管怎么说,你给的那个看起来不错。再次感谢。@PhillipMills基本上我没有得到我想要的。。所以我期待着找到合适的术语/我如何做到这一点的逻辑。。。因此,我发布了这张图片……我不知道怎么做,但你可以尝试在tableview部分添加主单元格(例如:天气设置、支持常见问题解答等),并将子单元格添加为普通单元格。非常感谢。。它起作用了。然而,我使用的是静态单元格,并且正在寻找一种方法,该方法与隐藏单元格一样简单。我不知道这是否是一个好方法,但它奏效了,我在下面发布了我的做法。Swift3解决方案,可扩展和可折叠UITableView的自定义类
- (void)viewDidLoad
{
 [super viewDidLoad];
 isExpanded = NO;
 self.cell2.hidden = YES;
 self.cell3.hidden = YES;
 self.cell4.hidden = YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section]== 1 && [indexPath row]==0)
{
    new1 =[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
    new2 =[NSIndexPath indexPathForRow:indexPath.row+2 inSection:indexPath.section];
    new3 =[NSIndexPath indexPathForRow:indexPath.row+3 inSection:indexPath.section];
    self.cell2 = [super tableView:tableView cellForRowAtIndexPath:new1];
    self.cell3 = [super tableView:tableView cellForRowAtIndexPath:new2];
    self.cell4 = [super tableView:tableView cellForRowAtIndexPath:new3];
    CATransition *animate = [CATransition animation];
    animate.type = kCATransitionMoveIn;
    animate.duration = 0.4;

    [self.cell2.layer addAnimation:animate forKey:nil];
    [self.cell3.layer addAnimation:animate forKey:nil];
    [self.cell4.layer addAnimation:animate forKey:nil];

    if (isExpanded == NO)
    {
        self.cell4.hidden = NO;
        self.cell2.hidden = NO;
        self.cell3.hidden = NO;
        isExpanded = YES;        }
    else
    {
        isExpanded = NO;
        self.cell4.hidden = YES;
        self.cell2.hidden = YES;
        self.cell3.hidden = YES;

    }
}
}