UITableViewGroup类型sectionheader悬

2017-11-01  本文已影响62人  fero2004

该方法只能实现指定的sectionheaderview

1.首先定义一个sectionheaderview的类

@interface TestSectionView : UIView

@property (nonatomic,assign)UITableView *tableView;
@property (nonatomic,assign)int section;

@end
#import "TestSectionView.h"

@implementation TestSectionView

- (void)setFrame:(CGRect)frame
{
    CGRect sectionRect = [self.tableView rectForSection:self.section];
    if(self.tableView.contentOffset.y >= sectionRect.origin.y)
    {
        frame = CGRectMake(frame.origin.x, self.tableView.contentOffset.y, CGRectGetWidth(frame), CGRectGetHeight(frame));
    }
    [super setFrame:frame];
}

//hack,系统会自动删除header头,这里把他加回来
- (void)removeFromSuperview
{
    [super removeFromSuperview];
    if(self.superview == nil && self.tableView)
    {
        [self.tableView addSubview:self];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

viewcontroller里还要实现

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect sectionRect = [self.tableView rectForSection:self.sectionView.section];
    if(self.tableView.contentOffset.y >= sectionRect.origin.y)
    {
        self.sectionView.frame = CGRectMake(self.sectionView.frame.origin.x, self.tableView.contentOffset.y, CGRectGetWidth(self.sectionView.frame), CGRectGetHeight(self.sectionView.frame));
    }
}

demo地址 https://github.com/fero2004/SectionheaderTest

上一篇 下一篇

猜你喜欢

热点阅读