UITableView动态展示

点击某行cell时展示此行的更多数据,如图所示。

TableView动态展示.gif

源代码如下:

#import "ViewController.h"

@interface ViewController ()
{
    //展示标题
    UITableView *tbView;
    NSArray *array;
    
    //展示详情
    UITableView *dTbView;
    NSArray *dArr;
    
    NSIndexPath *inPath;//标识选中的是第几行
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    tbView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
    tbView.delegate = self;
    tbView.dataSource = self;
    tbView.tag = 100;
    
    [self.view addSubview:tbView];

    //展示详情的tableview
    dTbView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    dTbView.delegate = self;
    dTbView.dataSource = self;
    
    dTbView.tag = 200;
    
    array = @[@{@"section":@"1111",@"row":@[@"1111",@"1111",@"1111"]},
              @{@"section":@"2222",@"row":@[@"2222",@"2222",@"2222"]},
              @{@"section":@"3333",@"row":@[@"3333",@"3333",@"3333"]},
              @{@"section":@"4444",@"row":@[@"4444",@"4444",@"4444"]}];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == tbView.tag)
    {
        return array.count;
    }
    else
    {
        return dArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath == inPath && tableView.tag == tbView.tag)
    {
        return 44 * (dArr.count + 1);
    }
    
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    
    if (tableView.tag == tbView.tag)
    {
        if (indexPath == inPath && dArr)
        {
            cell.contentView.backgroundColor = [UIColor redColor];

            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            btn.backgroundColor = [UIColor greenColor];
            [btn addTarget:self action:@selector(tapBtn) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:btn];
            
            CGRect frame = CGRectMake(0, 44, 320, 44 * dArr.count);
            [dTbView setFrame:frame];
            [cell.contentView addSubview:dTbView];

            [dTbView reloadData];
        }
        else
        {
            cell.contentView.backgroundColor = [UIColor lightGrayColor];
            cell.textLabel.text = array[indexPath.row][@"section"];
        }
    }
    else
    {
        cell.contentView.backgroundColor = [UIColor darkGrayColor];
        cell.textLabel.text = dArr[indexPath.row];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == tbView.tag)
    {
        if (!inPath)
        {
            inPath = indexPath;
            dArr = array[indexPath.row][@"row"];
        }

        [tbView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
        NSLog(@"详情选中 %@",dArr[indexPath.row]);
    }
}

- (void)tapBtn
{
    if (inPath)
    {
        dArr = nil;
        
        [tbView reloadRowsAtIndexPaths:@[inPath] withRowAnimation:UITableViewRowAnimationFade];
        
        inPath = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

你可能感兴趣的:(UITableView动态展示)