如何在UITableView - IOS中实现Expandable - 可折叠的UITableViewCell

时间:2014-07-31 10:26:16

标签: ios uitableview uibutton

我接近UITableview以获取单元格ExpandView。我想要实现这样的东西。

Exapand cell View

因此,UITableView是最好的方法或建议我任何好的实施方式,我也无法根据subview调整screenSize

6 个答案:

答案 0 :(得分:3)

可能还有另外一种方法可以实现这一点,但这就是我如何动态扩展UITableViewCell。这可以提供想法,您可以实施您的解决方案。

我在数据模型中保持行高:

- (void)viewDidLoad {
    [super viewDidLoad];

    //Data source
    datasource = [[NSMutableArray alloc] init];
    NSMutableDictionary *aDicti = [[NSMutableDictionary alloc] init];
    [aDicti setValue:@"a TEXT" forKey:@"text"];
    [aDicti setValue:@(50) forKey:@"cellheight"]; // here
}

选择更改后,只需更新数据源中的相关密钥。

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    [[datasource objectAtIndex:indexPath.row] setObject:@(50) forKey:@"cellheight"];
    [tableView endUpdates];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];
    [[datasource objectAtIndex:indexPath.row] setObject:@(200) forKey:@"cellheight"];
    [tableView endUpdates];
}

一旦[tableView endUpdates];已执行heightForRowAtIndexPathnumberOfRowsInSection委托方法,并使用数据源中的值自动调整单元格高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[[datasource objectAtIndex:indexPath.row] objectForKey:@"cellheight"] intValue];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return datasource.count;
}

如果您不想在数据源中保留行高,基本上可以应用此功能。

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    [tableView endUpdates];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];
    [tableView endUpdates];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.indexPathForSelectedRow.row == indexPath.row) {
        return 100;
    }
    return 50;
}

答案 1 :(得分:0)

SDNestedTable完全符合您的要求。

  

模块概念是具有的所有默认功能   一个UITableView及其单元格,同时为每个单元格添加   小孩UITableView。主要的每个单元格(SDGroupCell)   SDNestedTableViewController tableview充当自己的控制器   子表。表的状态,人口和行为   subtable主要由   SDNestedTableViewController

答案 2 :(得分:0)

这个电话,手风琴,这个网站有很好的例子(有演示),here's the link

答案 3 :(得分:0)

基本上,当您使用TableView时,您应该使用多个节标题和单元格。 如果要使用节标题,则在想要展开它时,应将numberOfRowsInSection设置为0(汇总)为X.之后打电话

tableView.reloadData

我在这里用动画实现了这种行为,具有不同的高度:

https://github.com/rudald/expandedTableViewIOSSwift/

答案 4 :(得分:0)

有许多关于此功能的开源项目。我已经下载了一些项目,对我来说最可定制和无问题的是SLExpandableTableView

答案 5 :(得分:0)

如果您正在寻找可扩展视图的直接易于设置的库,HVTableView是您的选择。它提供了可接受的性能,足以在常规项目中使用。

相关问题