Created
February 28, 2017 07:00
-
-
Save dvlprliu/72162ef0781d00ff1a571b9d8f18afb5 to your computer and use it in GitHub Desktop.
Make UITableView's section header not pined at top of each section
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef NS_ENUM(NSUInteger, SectionHeaderType) { | |
SectionHeaderTypeHeader, | |
SectionHeaderTypeFooter | |
}; | |
/** | |
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { | |
SectionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:REUSED_HEADER_ID]; | |
if (!header) { | |
header = [[SectionHeader alloc] initWithReuseIdentifier:REUSED_HEADER_ID]; | |
} | |
header.tableView = tableView; | |
header.section = section; | |
header.type = ABFavoriteListSectionTypeHeader; | |
return header; | |
} | |
*/ | |
@interface SectionHeader : UITableViewHeaderFooterView | |
@property (nonatomic, weak) UITableView *tableView; //incase of memory leak | |
@property (nonatomic, assign) NSUInteger section; | |
@property (nonatomic, assign) ABFavoriteListSectionType type; | |
@end | |
@interface ABFavoriteListSectionHeader () | |
@end | |
@implementation ABFavoriteListSectionHeader | |
- (void)setFrame:(CGRect)frame { | |
CGRect sectionRect = [self.tableView rectForSection:self.section]; | |
CGFloat top = 0; | |
switch (self.type) { | |
case SectionHeaderTypeHeader: | |
top = CGRectGetMinY(sectionRect); | |
break; | |
case SectionHeaderTypeFooter: | |
top = CGRectGetMaxY(sectionRect) - CGRectGetHeight(frame); | |
default: | |
break; | |
} | |
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), | |
top, | |
CGRectGetWidth(frame), | |
CGRectGetHeight(frame)); | |
[super setFrame:newFrame]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment