Skip to content

Commit

Permalink
Add the header and footer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenofex committed Jan 17, 2012
1 parent d297224 commit 95ce831
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
7 changes: 6 additions & 1 deletion StreamView/EKStreamView.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@

@optional

- (UIView *)headerForStreamView:(EKStreamView *)streamView;
- (UIView *)footerForStreamView:(EKStreamView *)streamView;

@optional

@end


Expand All @@ -54,11 +59,11 @@
NSMutableArray
*cellHeightsByIndex, // 1d
*cellHeightsByColumn, // 2d
*heightsForColumns, // 1d
*rectsForCells; // 2d EKWaterfallCellInfo

NSMutableDictionary *cellCache; // reuseIdentifier => NSMutableArray
NSSet *visibleCellInfo;
UIView *headerView, *footerView;
}

@property (nonatomic, assign) id<EKStreamViewDelegate> delegate;
Expand Down
38 changes: 31 additions & 7 deletions StreamView/EKStreamView.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,40 @@ - (void)dealloc
[[super delegate] release];
[cellHeightsByIndex release];
[cellHeightsByColumn release];
[heightsForColumns release];
[rectsForCells release];
[visibleCellInfo release];
[cellCache release];

[headerView release];
[footerView release];
[super dealloc];
}

- (void)reloadData
{
[cellHeightsByIndex removeAllObjects];
[cellHeightsByColumn removeAllObjects];
[heightsForColumns removeAllObjects];
[rectsForCells removeAllObjects];
[cellCache removeAllObjects];

if ([delegate respondsToSelector:@selector(headerForStreamView:)]) {
headerView = [[delegate headerForStreamView:self] retain];
CGRect f = headerView.frame;
f.origin = CGPointZero;
headerView.frame = f;

[self addSubview:headerView];
} else {
headerView = nil;
}

if ([delegate respondsToSelector:@selector(footerForStreamView:)]) {
footerView = [[delegate footerForStreamView:self] retain];
[self addSubview:footerView];
} else {
footerView = nil;
}

// calculate height for all cells
NSInteger numberOfColumns = [delegate numberOfColumnsInStreamView:self];
if (numberOfColumns < 1)
Expand All @@ -113,11 +132,12 @@ - (void)reloadData


CGFloat columnWidth = self.frame.size.width / numberOfColumns;
CGFloat cellHeight = headerView ? headerView.frame.size.height : 0.0f;
for (int i = 0; i < numberOfColumns; i++) {
[cellHeightsByColumn addObject:[NSMutableArray arrayWithCapacity:20]];
[rectsForCells addObject:[NSMutableArray arrayWithCapacity:20]];
[heightsForColumns addObject:[NSNumber numberWithFloat:0.0f]];
cellX[i] = (i == 0 ? 0.0f : cellX[i - 1] + columnWidth);
columnHeights[i] = cellHeight;
}

for (int i = 0; i < numberOfCells; i++) {
Expand Down Expand Up @@ -159,11 +179,16 @@ - (void)reloadData
maxHeight = columnHeights[i];
}

if (footerView) {
CGRect f = footerView.frame;
f.origin = CGPointMake(0.0f, maxHeight);
footerView.frame = f;

maxHeight += footerView.frame.size.height;
}

self.contentSize = CGSizeMake(0.0f, maxHeight);

for (int i = 0; i < numberOfColumns; i++) {
[heightsForColumns addObject:[NSNumber numberWithFloat:columnHeights[i]]];
}

free(columnHeights);
free(cellX);
Expand Down Expand Up @@ -226,7 +251,6 @@ - (void)setup

cellHeightsByIndex = [[NSMutableArray alloc] initWithCapacity:30];
cellHeightsByColumn = [[NSMutableArray alloc] initWithCapacity:5];
heightsForColumns = [[NSMutableArray alloc] initWithCapacity:5];
rectsForCells = [[NSMutableArray alloc] initWithCapacity:5];
cellCache = [[NSMutableDictionary alloc] initWithCapacity:5];
}
Expand Down
16 changes: 16 additions & 0 deletions StreamView/EKViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,20 @@ - (CGFloat)streamView:(EKStreamView *)streamView heightForCellAtIndex:(NSInteger
return [[randomHeights objectAtIndex:index] floatValue];
}

- (UIView *)headerForStreamView:(EKStreamView *)streamView
{
MyCell *header = [[[MyCell alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)] autorelease];
header.label.text = @"This is the header";

return header;
}

- (UIView *)footerForStreamView:(EKStreamView *)streamView
{
MyCell *footer = [[[MyCell alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)] autorelease];
footer.label.text = @"This is the footer";

return footer;
}

@end

0 comments on commit 95ce831

Please sign in to comment.