Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implementation for #16, Automatic Headers/Footers #72

Merged
merged 2 commits into from

2 participants

@jonsterling
Collaborator

I've added -gridView:titleForHeaderInSection: and -gridView:titleForFooterInSection: to <KKGridViewDataSource>. These should behave as expected.

Consumers may continue to provide their own views in the usual way if they desire to do so.

jonsterling added some commits
@jonsterling jonsterling Added automatic headers and footers
If a view is not provided by the data source, but a title is, it will be
used to generate a default view.
e1750f9
@jonsterling jonsterling Updated blocks delegate for titles (close #16)
Fixed a typo as well
f68d743
@kolinkrewinkel

Just curious, what do the automatic implementations look like?

@kolinkrewinkel kolinkrewinkel merged commit 255f582 into kolinkrewinkel:master
@jonsterling
Collaborator

Pretty much like the demo ones you wrote. I spruced up the colors a tiny bit, but basically the same. Here's a screenshot:

Defaults

@jonsterling
Collaborator

I think at some point, it may be beneficial to try to make the defaults look like UITableView, incidentally.

@kolinkrewinkel

Okay, that looks nice. Later on I'll change them to Apple's default style.

@jonsterling
Collaborator

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 17, 2011
  1. @jonsterling

    Added automatic headers and footers

    jonsterling authored
    If a view is not provided by the data source, but a title is, it will be
    used to generate a default view.
  2. @jonsterling

    Updated blocks delegate for titles (close #16)

    jonsterling authored
    Fixed a typo as well
This page is out of date. Refresh to see the latest.
View
2  Examples/GridViewDemo/GridViewDemo/GridViewDemoViewController.h
@@ -11,7 +11,5 @@
@interface GridViewDemoViewController : KKGridViewController <UISearchBarDelegate>
@property (nonatomic) NSUInteger firstSectionCount;
-@property (nonatomic, strong) NSMutableArray *footerViews;
-@property (nonatomic, strong) NSMutableArray *headerViews;
@end
View
26 Examples/GridViewDemo/GridViewDemo/GridViewDemoViewController.m
@@ -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;
@@ -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];
}
View
2  KKGridView/KKBlocksDelegate.h
@@ -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);
View
12 KKGridView/KKBlocksDelegate.m
@@ -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;
@@ -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;
View
2  KKGridView/KKGridView.h
@@ -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;
View
77 KKGridView/KKGridView.m
@@ -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;
@@ -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
@@ -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:)];
@@ -943,7 +951,7 @@ - (void)_commonReload
[views removeAllObjects];
};
- if (_dataSourceRespondsTo.heightForHeader && _dataSourceRespondsTo.viewForHeader) {
+ if (_dataSourceRespondsTo.viewForHeader || _dataSourceRespondsTo.titleForHeader) {
clearAuxiliaryViews(_headerViews);
if (!_headerViews)
{
@@ -951,7 +959,7 @@ - (void)_commonReload
}
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];
@@ -962,7 +970,7 @@ - (void)_commonReload
}
}
- if (_dataSourceRespondsTo.heightForFooter && _dataSourceRespondsTo.viewForFooter) {
+ if (_dataSourceRespondsTo.viewForFooter || _dataSourceRespondsTo.titleForFooter) {
clearAuxiliaryViews(_footerViews);
if (!_footerViews)
{
@@ -970,7 +978,7 @@ - (void)_commonReload
}
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];
@@ -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
Something went wrong with that request. Please try again.