UITableView在style为plain时,实现sectionHeader跟随cell移动

来源:互联网 发布:mac标点 编辑:程序博客网 时间:2024/05/17 01:15

自定义一个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;}
0 0