Skip to content

Commit

Permalink
Merge pull request #72 from jonsterling/issue16
Browse files Browse the repository at this point in the history
Implementation for #16, Automatic Headers/Footers
  • Loading branch information
kolinkrewinkel committed Dec 17, 2011
2 parents 21e3d4b + f68d743 commit 255f582
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 32 deletions.
Expand Up @@ -11,7 +11,5 @@
@interface GridViewDemoViewController : KKGridViewController <UISearchBarDelegate>

@property (nonatomic) NSUInteger firstSectionCount;
@property (nonatomic, strong) NSMutableArray *footerViews;
@property (nonatomic, strong) NSMutableArray *headerViews;

@end
26 changes: 4 additions & 22 deletions Examples/GridViewDemo/GridViewDemo/GridViewDemoViewController.m
Expand Up @@ -16,30 +16,12 @@

@implementation GridViewDemoViewController
@synthesize firstSectionCount = _firstSectionCount;
@synthesize footerViews = _footerViews;
@synthesize headerViews = _headerViews;

#pragma mark - View lifecycle

- (void)loadView
{
[super loadView];
_headerViews = [[NSMutableArray alloc] initWithCapacity:kNumSection];
_footerViews = [[NSMutableArray alloc] initWithCapacity:kNumSection];

for (NSUInteger section = 0; section < kNumSection; section++) {
UILabel *header = [[UILabel alloc] initWithFrame:CGRectZero];
header.backgroundColor = [UIColor grayColor];
header.textAlignment = UITextAlignmentCenter;
header.text = [NSString stringWithFormat:@"Header %d", section + 1];
[_headerViews addObject:header];

UILabel *footer = [[UILabel alloc] initWithFrame:CGRectZero];
footer.textAlignment = UITextAlignmentCenter;
footer.text = [NSString stringWithFormat:@"Footer %d", section + 1];
footer.backgroundColor = [UIColor colorWithRed:0.772f green:0.788f blue:0.816f alpha:1.f];
[_footerViews addObject:footer];
}

_firstSectionCount = 7;

Expand Down Expand Up @@ -118,14 +100,14 @@ - (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)
return 25.f;
}

- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section
- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section
{
return [self.headerViews objectAtIndex:section];
return [NSString stringWithFormat:@"Header %i",section];
}

- (UIView *)gridView:(KKGridView *)gridView viewForFooterInSection:(NSUInteger)section
- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section
{
return [self.footerViews objectAtIndex:section];
return [NSString stringWithFormat:@"Footer %i",section];
}


Expand Down
2 changes: 2 additions & 0 deletions KKGridView/KKBlocksDelegate.h
Expand Up @@ -18,6 +18,8 @@
@property (copy) NSUInteger (^numberOfItems)(KKGridView *gridView, NSUInteger section);
@property (copy) CGFloat (^heightForHeader)(KKGridView *gridView, NSUInteger section);
@property (copy) CGFloat (^heightForFooter)(KKGridView *gridView, NSUInteger section);
@property (copy) NSString *(^titleForHeader)(KKGridView *gridView, NSUInteger section);
@property (copy) NSString *(^titleForFooter)(KKGridView *gridView, NSUInteger section);
@property (copy) UIView *(^viewForHeader)(KKGridView *gridView, NSUInteger section);
@property (copy) UIView *(^viewForFooter)(KKGridView *gridView, NSUInteger section);

Expand Down
12 changes: 12 additions & 0 deletions KKGridView/KKBlocksDelegate.m
Expand Up @@ -14,6 +14,8 @@ @implementation KKBlocksDelegate
@synthesize numberOfItems = _numberOfItems;
@synthesize heightForHeader = _heightForHeader;
@synthesize heightForFooter = _heightForFooter;
@synthesize titleForHeader = _titleForHeader;
@synthesize titleForFooter = _titleForFooter;
@synthesize viewForHeader = _viewForHeader;
@synthesize viewForFooter = _viewForFooter;

Expand All @@ -40,6 +42,16 @@ - (NSUInteger)numberOfSectionsInGridView:(KKGridView *)gridView
return _numberOfSections ? _numberOfSections(gridView) : 1;
}

- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section
{
return _titleForHeader ? _titleForFooter(gridView,section) : @"";
}

- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section
{
return _titleForFooter ? _titleForFooter(gridView,section) : @"";
}

- (CGFloat)gridView:(KKGridView *)gridView heightForHeaderInSection:(NSUInteger)section
{
return _heightForHeader ? _heightForHeader(gridView,section) : 25.0;
Expand Down
2 changes: 2 additions & 0 deletions KKGridView/KKGridView.h
Expand Up @@ -92,6 +92,8 @@ typedef enum {
- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath;
@optional
- (NSUInteger)numberOfSectionsInGridView:(KKGridView *)gridView;
- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section;
- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section;
- (CGFloat)gridView:(KKGridView *)gridView heightForHeaderInSection:(NSUInteger)section;
- (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)section;
- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section;
Expand Down
77 changes: 69 additions & 8 deletions KKGridView/KKGridView.m
Expand Up @@ -48,6 +48,8 @@ @interface KKGridView () {
// DataSource/Delegate flags
struct {
unsigned int numberOfSections:1;
unsigned int titleForHeader:1;
unsigned int titleForFooter:1;
unsigned int heightForHeader:1;
unsigned int heightForFooter:1;
unsigned int viewForHeader:1;
Expand Down Expand Up @@ -105,6 +107,10 @@ - (void)_selectItemAtIndexPath:(KKIndexPath *)indexPath;
- (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath;
- (void)_handleSelection:(UITapGestureRecognizer *)recognizer;

// Headers and Footer views
- (UIView *)_viewForHeaderInSection:(NSUInteger)section;
- (UIView *)_viewForFooterInSection:(NSUInteger)section;

@end

@implementation KKGridView
Expand Down Expand Up @@ -188,6 +194,8 @@ - (void)setDataSource:(id<KKGridViewDataSource>)dataSource
{
_dataSource = dataSource;
_dataSourceRespondsTo.numberOfSections = [_dataSource respondsToSelector:@selector(numberOfSectionsInGridView:)];
_dataSourceRespondsTo.titleForHeader = [_dataSource respondsToSelector:@selector(gridView:titleForHeaderInSection:)];
_dataSourceRespondsTo.titleForFooter = [_dataSource respondsToSelector:@selector(gridView:titleForFooterInSection:)];
_dataSourceRespondsTo.heightForHeader = [_dataSource respondsToSelector:@selector(gridView:heightForHeaderInSection:)];
_dataSourceRespondsTo.heightForFooter = [_dataSource respondsToSelector:@selector(gridView:heightForFooterInSection:)];
_dataSourceRespondsTo.viewForHeader = [_dataSource respondsToSelector:@selector(gridView:viewForHeaderInSection:)];
Expand Down Expand Up @@ -943,15 +951,15 @@ - (void)_commonReload
[views removeAllObjects];
};

if (_dataSourceRespondsTo.heightForHeader && _dataSourceRespondsTo.viewForHeader) {
if (_dataSourceRespondsTo.viewForHeader || _dataSourceRespondsTo.titleForHeader) {
clearAuxiliaryViews(_headerViews);
if (!_headerViews)
{
_headerViews = [[NSMutableArray alloc] initWithCapacity:_numberOfSections];
}

for (NSUInteger section = 0; section < _numberOfSections; section++) {
UIView *view = [_dataSource gridView:self viewForHeaderInSection:section];
UIView *view = [self _viewForHeaderInSection:section];
KKGridViewHeader *header = [[KKGridViewHeader alloc] initWithView:view];
[_headerViews addObject:header];

Expand All @@ -962,15 +970,15 @@ - (void)_commonReload
}
}

if (_dataSourceRespondsTo.heightForFooter && _dataSourceRespondsTo.viewForFooter) {
if (_dataSourceRespondsTo.viewForFooter || _dataSourceRespondsTo.titleForFooter) {
clearAuxiliaryViews(_footerViews);
if (!_footerViews)
{
_footerViews = [[NSMutableArray alloc] initWithCapacity:_numberOfSections];
}

for (NSUInteger section = 0; section < _numberOfSections; section++) {
UIView *view = [_dataSource gridView:self viewForFooterInSection:section];
UIView *view = [self _viewForFooterInSection:section];
KKGridViewFooter *footer = [[KKGridViewFooter alloc] initWithView:view];
[_footerViews addObject:footer];

Expand Down Expand Up @@ -1038,15 +1046,68 @@ - (void)_reloadMetrics

for (_metricsCount = 0; _metricsCount < _numberOfSections; _metricsCount++)
{
struct KKSectionMetrics *sectionMetrics = &_metrics[_metricsCount];
struct KKSectionMetrics sectionMetrics = {
.footerHeight = 25.f,
.headerHeight = 25.f,
.sectionHeight = 0.f,
.itemCount = 0.f
};

sectionMetrics->itemCount = [_dataSource gridView:self numberOfItemsInSection:_metricsCount];
sectionMetrics.itemCount = [_dataSource gridView:self numberOfItemsInSection:_metricsCount];

if (_dataSourceRespondsTo.heightForHeader)
sectionMetrics->headerHeight = [_dataSource gridView:self heightForHeaderInSection:_metricsCount];
sectionMetrics.headerHeight = [_dataSource gridView:self heightForHeaderInSection:_metricsCount];
if (_dataSourceRespondsTo.heightForFooter)
sectionMetrics->footerHeight = [_dataSource gridView:self heightForFooterInSection:_metricsCount];
sectionMetrics.footerHeight = [_dataSource gridView:self heightForFooterInSection:_metricsCount];

_metrics[_metricsCount] = sectionMetrics;
}
}

#pragma mark - Header and Footer Views

- (UIView *)_viewForHeaderInSection:(NSUInteger)section
{
NSAssert(_dataSourceRespondsTo.viewForHeader || _dataSourceRespondsTo.titleForHeader, @"DataSource must provide title or view for header.");

UIView *headerView = nil;

if (_dataSourceRespondsTo.viewForHeader) {
headerView = [_dataSource gridView:self viewForHeaderInSection:section];
}

if (!headerView && _dataSourceRespondsTo.titleForHeader) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor darkGrayColor];
label.textColor = [UIColor lightTextColor];
label.textAlignment = UITextAlignmentCenter;
label.text = [_dataSource gridView:self titleForHeaderInSection:section];
headerView = label;
}

return headerView;
}

- (UIView *)_viewForFooterInSection:(NSUInteger)section
{
NSAssert(_dataSourceRespondsTo.viewForFooter || _dataSourceRespondsTo.titleForFooter, @"DataSource must provide title or view for footer.");

UIView *footerView = nil;

if (_dataSourceRespondsTo.viewForFooter) {
footerView = [_dataSource gridView:self viewForFooterInSection:section];
}

if (!footerView && _dataSourceRespondsTo.titleForFooter) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor colorWithRed:0.772f green:0.788f blue:0.816f alpha:1.f];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor darkGrayColor];
label.text = [_dataSource gridView:self titleForFooterInSection:section];
footerView = label;
}

return footerView;
}

#pragma mark - Positioning
Expand Down

0 comments on commit 255f582

Please sign in to comment.