自定义一个SectionHeaderView,继承自UIView
.h方法
import <UIKit/UIKit.h>
@interface SectionHeaderView : UIView
@property NSUInteger section;
@property (nonatomic, weak) UITableView *tableView;
@end
.m方法
import "SectionHeaderView.h"
@implementation SectionHeaderView
- (void)setFrame:(CGRect)frame{
CGRect sectionRect = [self.tableView rectForSection:self.section];
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame));
[super setFrame:newFrame];
}
@end
使用的时候:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SectionHeaderView *sectionHead = [[SectionHeaderView alloc] init];
sectionHead.section = section;
sectionHead.tableView = tableView;
return sectionHead;
}