|
//
// HeaderInsetTableView.m
// ThePaperHD
//
// Created by scar1900 on 15/5/11.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "HeaderInsetTableView.h"
@interface HeaderInsetTableView ()
{
BOOL shouldManuallyLayoutHeaderViews;
}
- (void) layoutHeaderViews;
@end
@implementation HeaderInsetTableView
@synthesize headerViewInsets;
- (void) layoutSubviews
{
[super layoutSubviews];
if(shouldManuallyLayoutHeaderViews)
[self layoutHeaderViews];
}
- (void) setHeaderViewInsets:(UIEdgeInsets)_headerViewInsets
{
headerViewInsets = _headerViewInsets;
shouldManuallyLayoutHeaderViews = !UIEdgeInsetsEqualToEdgeInsets(headerViewInsets, UIEdgeInsetsZero);
[self setNeedsLayout];
}
#pragma mark -
#pragma mark Private
- (void) layoutHeaderViews
{
const NSUInteger numberOfSections = self.numberOfSections;
const UIEdgeInsets contentInset = self.contentInset;
const CGPoint contentOffset = self.contentOffset;
const CGFloat sectionViewMinimumOriginY = contentOffset.y + contentInset.top + headerViewInsets.top;
// Layout each header view
for(NSUInteger section = 0; section < numberOfSections; section++)
{
UIView* sectionView = [self headerViewForSection:section];
if(sectionView == nil)
continue;
const CGRect sectionFrame = [self rectForSection:section];
CGRect sectionViewFrame = sectionView.frame;
sectionViewFrame.origin.y = ((sectionFrame.origin.y < sectionViewMinimumOriginY) ? sectionViewMinimumOriginY : sectionFrame.origin.y);
// If it's not last section, manually 'stick' it to the below section if needed
if(section < numberOfSections - 1)
{
const CGRect nextSectionFrame = [self rectForSection:section + 1];
if(CGRectGetMaxY(sectionViewFrame) > CGRectGetMinY(nextSectionFrame))
sectionViewFrame.origin.y = nextSectionFrame.origin.y - sectionViewFrame.size.height;
}
// TPLOG(@"%f",sectionViewFrame.origin.y);
[sectionView setFrame:sectionViewFrame];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
|