#import <UIKit/UIKit.h>
@interface XOSectionView : UITableViewHeaderFooterView
@property NSUInteger section;
@property (nonatomic, copy) NSString *titleName;
@property (nonatomic, weak) UITableView *tableView;
+ (CGFloat)getSectionHeight;
@end
#import "XOSectionView.h"
#define APP_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width // 屏幕的宽度
@interface XOSectionView()
@property (nonatomic, weak) UILabel *titleLabel;
@end
@implementation XOSectionView
-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, APP_SCREEN_WIDTH - 20, 20)];
titleLabel.textColor = [UIColor colorWithRed:0.35f green:0.35f blue:0.35f alpha:1.00f];
titleLabel.font = [UIFont systemFontOfSize:15.0f];
_titleLabel = titleLabel;
self.backgroundColor = [UIColor whiteColor];
// [self addSubview:titleLabel];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
view.backgroundColor = [UIColor whiteColor];
[view addSubview:titleLabel];
[self addSubview:view];
}
return self;
}
- (void)setTitleName:(NSString *)titleName {
_titleLabel.text = titleName;
}
+ (CGFloat)getSectionHeight
{
return 44;
}
- (void)setFrame:(CGRect)frame{
// NSLog(@"_______ frame = %@",NSStringFromCGRect(frame));
CGRect sectionRect = [self.tableView rectForSection:self.section];
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame)); [super setFrame:newFrame];
}
@end
#import "ViewController.h"
#import "SubViewController.h"
#import "XOSectionView.h"
static NSString *SectionViewID = @"XOSectionView";
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)btnClick:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[XOSectionView class] forHeaderFooterViewReuseIdentifier:SectionViewID];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellName = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.textLabel.text = @"test";
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
XOSectionView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionViewID];
if (sectionView) {
if (section == 1) {
sectionView.titleName = @"城市服务";
} else {
sectionView.titleName = @"联盟商家";
}
sectionView.tableView = self.tableView;
sectionView.section = section;
}
return sectionView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnClick:(UIButton *)sender {
SubViewController *subVC = [[SubViewController alloc] init];
subVC.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:subVC animated:true];
}
@end