1:自定义headerView
.h文件 // // MyHeaderView.h // GPS_Clock_Offline // // Created by aslan on 16/4/13. // Copyright © 2016年 aslan. All rights reserved. // #import <UIKit/UIKit.h> @class MyHeaderView; @protocol MyHeaderViewDelegate <NSObject> @optional - (void)headerView :(MyHeaderView *)headerView section :(NSInteger)section expanded:(BOOL)expanded; @end @interface MyHeaderView : UIView <UIGestureRecognizerDelegate> //是不是出于点击状态 @property (nonatomic,assign,readwrite)BOOL expanded; //处于哪一个组 @property (nonatomic,assign)NSInteger section; //单击手势 @property (nonatomic,strong)UITapGestureRecognizer * singleTapGestureRecognizer; - (id)initWithFrame:(CGRect)frame expanded:(BOOL) expanded; //引入代理 @property (nonatomic,strong)id<MyHeaderViewDelegate> delegate; @end .m文件 // // MyHeaderView.m // GPS_Clock_Offline // // Created by aslan on 16/4/13. // Copyright © 2016年 aslan. All rights reserved. // #import "MyHeaderView.h" @implementation MyHeaderView - (id)initWithFrame:(CGRect)frame expanded:(BOOL)expanded { if (self = [super initWithFrame:frame]) { self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.backgroundColor = [UIColor lightGrayColor]; self.expanded = expanded; //设置单击手势操作 [self setupTapGestureRecognizer]; } return self; } - (void)setupTapGestureRecognizer { self.singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGesture:)]; self.singleTapGestureRecognizer.delegate = self; [self addGestureRecognizer:self.singleTapGestureRecognizer]; } //响应单击手势 - (void)singleTapGesture:(UITapGestureRecognizer *)tap { //更新数据 self.expanded = !self.expanded; //通知代理 if (self.delegate != nil && [self.delegate respondsToSelector:@selector((headerView:section:expanded:))]) { [self.delegate headerView:self section:self.section expanded:self.expanded]; } } @end
2:被点击的cell显示出来的是它的headerView。
// // OfflineViewController.m // GPS_Clock_Offline // // Created by aslan on 16/4/10. // Copyright © 2016年 aslan. All rights reserved. // #import "OfflineViewController.h" #import "MyHeaderView.h" @interface OfflineViewController () <UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,MyHeaderViewDelegate> /*创建一个搜索栏*/ @property (nonatomic,strong)UISearchBar * searchBar; /*创建一个tableView*/ @property (strong, nonatomic) UITableView *tableView; @property (nonatomic,assign)BOOL isExpend; @property (nonatomic,assign)char * expandedSections; @end @implementation OfflineViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.isExpend = NO; _expandedSections = (char *)malloc(10 * sizeof(char)); memset(_expandedSections, 0, (10)* sizeof(char)); [self initSearchBar]; [self initTableView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewWillAppear:(BOOL)animated { } #pragma mark 实现MyHeaderViewDelegate - (void)headerView:(MyHeaderView *)headerView section:(NSInteger)section expanded:(BOOL)expanded { NSLog(@"section = %ld",section); // self.isExpend = YES; _expandedSections[section] = expanded; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone]; } #pragma mark初始化 搜索栏 - (void)initSearchBar { _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _searchBar.barStyle = UIBarStyleBlack; _searchBar.delegate = self; _searchBar.placeholder = @"请输入要搜索的地址"; _searchBar.keyboardType = UIKeyboardTypeDefault; //添加搜索栏到NavigationItem的titleView上面 self.navigationItem.titleView = self.searchBar; [self.searchBar sizeToFit]; } #pragma mark 搜索栏的代理 /*使用这个就可以实现点击搜索栏时,跳转到另一个界面,然后返回这个界面是,搜索栏不是出于搜索编辑状态*/ - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { // NSLog(@"start Edit"); // self.isExpend = YES; // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone]; return NO; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { } #pragma mark 初始化tableView - (void)initTableView { _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))]; [self.view addSubview:_tableView]; _tableView.dataSource = self; _tableView.delegate = self; } #pragma mark tableView的代理 #pragma mark - 数据源设置 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /* NSString * indetify=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indetify]; if (cell==nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify]; } cell.textLabel.text=[dataArray objectAtIndex:indexPath.row]; cell.accessoryType=UITableViewCellAccessoryCheckmark;//添加附加的样子 return cell; */ NSString * reusedId = @"address"; //1.创建cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reusedId]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId]; } //2.给cell赋值 cell.textLabel.text = @"hello"; // cell.detailTextLabel.text =tip.district; //3.返回cell return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger number = 0; switch (section) { case 0: if (_expandedSections[section] == YES) number = 2; break; case 1: if (_expandedSections[section] == YES) number = 3; break; default: if (_expandedSections[section] == YES) number = 4; break; } return number; } #pragma mark - tabelView的代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } //设置titleHeader的高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 22.f; } //设置titleHeader的内容 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 20.f)]; // headerView.backgroundColor = [UIColor lightGrayColor]; // // UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 0, CGRectGetWidth(headerView.bounds), CGRectGetHeight(headerView.bounds))]; // lb.backgroundColor = [UIColor redColor]; // lb.text = @"title"; // lb.textColor = [UIColor blackColor]; // // [headerView addSubview:lb]; // // return headerView; MyHeaderView *headerView = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 20.f) expanded:_expandedSections[section]]; headerView.section = section; headerView.delegate = self; UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 0, CGRectGetWidth(headerView.bounds), CGRectGetHeight(headerView.bounds))]; lb.backgroundColor = [UIColor redColor]; lb.text = @"title"; lb.textColor = [UIColor blackColor]; [headerView addSubview:lb]; return headerView; } @end