Skip to content

Instantly share code, notes, and snippets.

@dvlprliu
Created February 28, 2017 07:00
Show Gist options
  • Save dvlprliu/72162ef0781d00ff1a571b9d8f18afb5 to your computer and use it in GitHub Desktop.
Save dvlprliu/72162ef0781d00ff1a571b9d8f18afb5 to your computer and use it in GitHub Desktop.
Make UITableView's section header not pined at top of each section
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