美文网首页
UITableView动态展示

UITableView动态展示

作者: PZcoder | 来源:发表于2017-08-10 16:58 被阅读16次

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

TableView动态展示.gif

源代码如下:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    //展示标题
    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动态展示

    点击某行cell时展示此行的更多数据,如图所示。 源代码如下:

  • CS193p 斯坦福IOS开发 2011 (九)

    这节课讲了如何展示动态数据列表/固定数据列表 UITableView 一维表 UIScrollView子类 可以是...

  • UITableView

    UITableView基本使用UITableView回到顶部动态改变 TableHeaderView 高度

  • UITableView

    UITableView 创建UITableView 代码 使用Masonry布局 storyboard动态单元格记...

  • 平滑调整Cell的高度

    前提是你的UITableView是根据内容的多少动态调整Cell高度的。当Cell要展示的内容发生变化时,可用下面...

  • iOS中的常用UI控件(二):TableView

    一、基本属性 UITableView : 能够实现表格数据展示UITableView继承自UIScrollView...

  • UITableView01-基础知识

    UITableView UITableView如何展示数据设置dataSource数据源数据源需要遵守UITabl...

  • UITableView

    什么是UITableView - 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView - ...

  • iOS学习之UITableView

    UITableView UITableView继承自UIScrollView,可以用来展示一组或多组内容样式相似的...

  • 玩转UITableView

    初识UITableView 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView UITab...

网友评论

      本文标题:UITableView动态展示

      本文链接:https://www.haomeiwen.com/subject/hhwhrxtx.html