简体   繁体   中英

How to implement Expandable - Collapsable UITableViewCell in UITableView - IOS

I took approach of the UITableview to get cell click ExpandView . I want something like this to be implement.

Exapand单元格视图

So, UITableView would be best approach for this or suggest me any good way of implementing, also I am not able to get subview to adjust according to screenSize .

Could be there are another ways to accomplish this but this is how I am expand UITableViewCell on the fly. This could give the idea and you can implement your solution.

I keep row heights in my data model:

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

When selection changed, just updating related key in data source.

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

Once [tableView endUpdates]; executed heightForRowAtIndexPath and numberOfRowsInSection delegate methods fired and automatically adjust cell height with the value from data source.

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

If you do not want to keep row height in your data source you can basically apply this.

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

SDNestedTable does exactly what you want.

The module concept is that of having all the default functionality of a UITableView and its cells while at the same time adding for each cell a child UITableView . Each cell ( SDGroupCell ) in the main SDNestedTableViewController tableview acts as controller for its own sub table. The state, population and behavior of the table and subtable is instead mostly controlled by SDNestedTableViewController .

这就是手风琴 ,这个网站有很好的例子(有演示), 这里是链接

Basically when you are working with TableView you should play with number of section headers and cells. In case of using section headers you should set numberOfRowsInSection to 0 (roll up) to X when you want to expand it. After that call

tableView.reloadData

I implemented this behavior here, with animation, with different heights:

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

There are numerous open source projects regarding this feature. I've downloaded a few projects and the most customizable and issue-less, for me, was SLExpandableTableView

If you're looking for a straight forward easy-to-setup library for expandable views, HVTableView is your choice. It provides an acceptable performance which is sufficient for using in regular projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM