UITableView動(dòng)態(tài)展示

點(diǎn)擊某行cell時(shí)展示此行的更多數(shù)據(jù),如圖所示。

TableView動(dòng)態(tài)展示.gif

源代碼如下:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    //展示標(biāo)題
    UITableView *tbView;
    NSArray *array;
    
    //展示詳情
    UITableView *dTbView;
    NSArray *dArr;
    
    NSIndexPath *inPath;//標(biāo)識(shí)選中的是第幾行
}
@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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容