无法同时添加和删除行

时间:2015-03-11 16:09:04

标签: ios objective-c uitableview

我正在学习一个教程,我可以通过添加一些子单元扩展表视图,并通过删除子单元来折叠表。我试图改变扩展操作应该如何执行。当我点击一行时,它会展开并显示子单元格,当我点击其他行时,前一个展开的行应该关闭。我无法做到这一点。我试过以下但是我不能一次扩展一行,而另一行应该在扩展时关闭。

注意:此代码工作正常,但要折叠该行,我们需要在同一行上点击两次。我正试图在其他父母被挖掘时崩溃。

以下是代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

    Node *node = [self nodeForIndexPath:indexPath];



// differenet table view cell prototypes for different node levels

UITableViewCell *cell;
if(node.level  == 0)

{
    cell = [tableView dequeueReusableCellWithIdentifier:@"level1cell" forIndexPath:indexPath];
}
else
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"level2cell" forIndexPath:indexPath];
}

// set the nodes title as row text
cell.textLabel.text = node.title;

// attach the nodeId for later lookup when selected
cell.tag = node.nodeId;

// Configure the cell...

return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
Node *node = [self nodeForIndexPath:indexPath];
  //  NSLog(@"node id is %ld level is %ld and indexpath.row is %d",(long)node.nodeId,(long)node.level,indexPath.row);


    node.expanded = !node.expanded;

if (node.level==0) {
    NSLog(@"you tapped parent");
    //now check other parents are expanded or not
}else{

    NSLog(@"you tapped child");
}

//insertion always happen after deletion
//   NSLog(@"you touched at %@,index row is %d",indexPath,indexPath.row);
if(node.expanded )
{

    // add n rows
    NSMutableArray *indexPaths = [NSMutableArray array];
    for(NSInteger i=indexPath.row; i< indexPath.row+node.subNodes.count; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
    }

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}
else
{

    //you store the node refe after row is expanded

    // delete n rows
    NSMutableArray *indexPaths = [NSMutableArray array];
    for(NSInteger i=indexPath.row; i< indexPath.row+node.subNodes.count; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
    }

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}

}





 #pragma mark - Private helper

  -(Node*) nodeForIndexPath:(NSIndexPath*)indexPath
  {
int idx = 0;

for(Node *node in _nodes)
{
    if(idx == indexPath.row)
    {
        return node;
    }
    idx++;

    if(node.expanded)
    {
        for (Node *subNode in node.subNodes)
        {
            if(idx == indexPath.row)
            {
                return subNode;
            }
            idx++;
        }
    }
}
return nil;
}

2 个答案:

答案 0 :(得分:0)

KMAccordionTableViewController可以帮到你吗?

https://github.com/klevison/KMAccordionTableViewController

答案 1 :(得分:0)

嗯...如果您当前的代码正在运行,但您需要点击两次以折叠打开/选定的行,也许是因为didDeselectRowAtIndexPath:在第一次点击期间被调用didSelectRowAtIndexPath:代替didSelectRowAtIndexPath: }以取消选择所选行。我建议配置您的tableview以允许多次选择,以便每次调用- (void)viewDidLoad { [super viewDidLoad]; tableView.allowsMultipleSelection = YES; } ,例如:

{{1}}