點擊某行cell時展示此行的更多數(shù)據(jù)审葬,如圖所示。
源代碼如下:
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
//展示標(biāo)題
UITableView *tbView;
NSArray *array;
//展示詳情
UITableView *dTbView;
NSArray *dArr;
NSIndexPath *inPath;//標(biāo)識選中的是第幾行
}
@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