闲着无聊.瞎写.目前貌似有点简单了...毕竟刚入门时困扰了好久...
#import "CeShiTableViewController.h"
@interface CeShiTableViewController ()
@property (nonatomic , strong) NSArray * titleAry;
@property (nonatomic , strong) NSArray *contentAry;
@property (nonatomic , strong) NSMutableArray *SelectAry;
@end
@implementation CeShiTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.titleAry = @[@"组1",@"组2",@"组3"];
self.contentAry = @[@[@"123",@"1234",@"111",@"666",@"dsadasd",@"dasfsa",@"2年前第一次写觉得好难",@"2年后再写实在有点简单了"],@[@"2",@"222",@"2222222",@"21"],@[@"UI股",@"湿哒哒",@"多撒大所",@"大神的撒范德萨范德萨范德萨发",@"sfdsf",@"wfwfwef",@"fs"]];
self.SelectAry = [@[@(0),@(0),@(0)]mutableCopy];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.tableView.backgroundColor = [UIColor redColor];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return _titleAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
if ([self.SelectAry[section] isEqualToNumber:@(0)]) {//没点击
return 0;
}else
return ((NSArray * )self.contentAry[section]).count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = self.contentAry[indexPath.section][indexPath.row];
cell.backgroundColor = [UIColor yellowColor];
// Configure the cell...
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *headerBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[headerBtn setTitle:self.titleAry[section] forState:UIControlStateNormal];
headerBtn.tag = 1000 + section;
[headerBtn addTarget:self action:@selector(ChangeNum:) forControlEvents:UIControlEventTouchUpInside];
headerBtn.backgroundColor = [UIColor blackColor];
return headerBtn;
}
- (void)ChangeNum:(UIButton *)sender{
int i = (int)sender.tag - 1000;
if ([self.SelectAry[i] isEqualToNumber:@(0)]) {//没展开
[self.SelectAry replaceObjectAtIndex:i withObject:@(1)];//去展开
}else//展开了
{
[self.SelectAry replaceObjectAtIndex:i withObject:@(0)];
}
//刷新tableview(区)
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation: UITableViewRowAnimationFade];
NSLog(@"点击数组%@",self.SelectAry);
}