如何在主视图上添加详细视图?

时间:2014-12-01 17:24:16

标签: ios objective-c uitableview master-detail

我想将详细信息视图放在主设备中,即只有一个动态视图。当用户点击一个单元格时,详细信息视图将显示在单元格下方,剩余的单元格将被向下推。

我可以这样做吗?

This is the master view

当用户点击单元格1时,单元格的详细视图将显示在cell1和cell2之间,如下所示。单元格2,3将被向下推动以显示数据。

detail view in master view

这可以在iOS中使用吗?

2 个答案:

答案 0 :(得分:1)

今天我有空闲时间写了一个扩展和折叠的完整示例。在简单的情况下,您可以简单地修改硬编码数据,但也可以在运行时加载结构。 我把它放在Github上,它也可能对其他人有用: https://github.com/efalkenberg/ExpandableTableView

答案 1 :(得分:0)

使用指示应显示哪种类型的数组,您可以动态插入3个单元格,如下所示:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // old value = @[@0,@0];
    self.data = @[@0,@1,@1,@1,@0];

    NSMutableArray *indexPaths = [NSMutableArray array];
    for(NSInteger i=indexPath.row; i< indexPath.row+3; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
    }

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

将通过数据源协议调用以插入单元格。

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

    if([self.data[indexPath.row]  isEqual: @0]){
        cell = [tableView dequeueReusableCellWithIdentifier:@"level1cell" forIndexPath:indexPath];
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"level2cell" forIndexPath:indexPath];
    }

    // Configure the cell...    
    return cell;
}

enter image description here

PS:我使用带有1和0的数组来使代码易于阅读,你绝对应该用更优雅的东西替换它:)

相关问题