【问题标题】:Unable to add and delete rows simultaneously无法同时添加和删除行
【发布时间】:2015-03-11 16:09:03
【问题描述】:

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

注意:此代码运行良好,但要折叠行,我们需要在同一行上点击两次。当其他父母被点击时,我试图崩溃。

代码如下:

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    KMAccordionTableViewController 能帮到你吗?

    https://github.com/klevison/KMAccordionTableViewController

    【讨论】:

    • 我尽一切努力添加故事板,但我的表格视图上没有显示任何内容
    • 为什么不呢?它也支持故事板。
    • 我添加了一个表格视图控制器,其中有两个单元格。一个是父母,一个是孩子。我删除了应用委托中的窗口分配。
    • 但我的表格视图中没有显示任何内容。
    • 我可以把它贴在这里吗?
    【解决方案2】:

    嗯...如果您当前的代码正在运行,但您需要点击两次以折叠打开/选定的行,这可能是因为在第一次点击期间调用 didDeselectRowAtIndexPath: 代替 didSelectRowAtIndexPath: 以便取消选择选定的行。我建议配置您的表格视图以允许多项选择,以便每次都调用didSelectRowAtIndexPath:,例如:

    - (void)viewDidLoad {
        [super viewDidLoad];
        tableView.allowsMultipleSelection = YES;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多