Skip to content

Commit

Permalink
A couple performance enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
camh committed Feb 23, 2010
1 parent a0a5086 commit 1d1d2cb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
9 changes: 5 additions & 4 deletions CHGridLayout/CHGridLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ static CHSectionRange CHSectionRangeMake(int start, int end){
NSMutableArray *_index;
NSMutableArray *_sectionTitles;

CGFloat gridWidth;
CGFloat contentHeight;
float gridWidth;
float contentHeight;
CGSize padding;
int perLine;
CGFloat rowHeight;
CGFloat sectionTitleHeight;
float rowHeight;
float pixelMargin;
float sectionTitleHeight;
BOOL dynamicallyResizeTilesToFillSpace;
}

Expand Down
17 changes: 8 additions & 9 deletions CHGridLayout/CHGridLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ - (void)dealloc{

#pragma mark setters

- (void)setRowHeight:(CGFloat)f{
rowHeight = f;
pixelMargin = f * 2;
}

- (void)setSections:(int)sections{
[self clearData];

Expand Down Expand Up @@ -123,13 +128,12 @@ - (void)updateLayout{
- (int)sectionIndexForContentOffset:(CGFloat)offset{
int sectionIndex = 0;

int i;
for(i = 0; i < _sectionTitles.count; i++){
CHGridLayoutSection *section = [_sectionTitles objectAtIndex:i];
for(CHGridLayoutSection *section in _sectionTitles){
if(section.yCoordinate <= offset && offset > 0){
sectionIndex = i;
sectionIndex = section.section;
}
}

return sectionIndex;
}

Expand All @@ -142,7 +146,6 @@ - (CHSectionRange)sectionRangeForContentOffset:(CGFloat)offset andHeight:(CGFloa
int end = 0;

BOOL firstRun = YES;
float pixelMargin = rowHeight * 2;
int currentSection = [self sectionIndexForContentOffset:offset];

for(CHGridLayoutSection *section in _sectionTitles){
Expand All @@ -168,9 +171,7 @@ - (CGRect)tileFrameForIndexPath:(CHGridIndexPath)indexPath{
}

- (CHGridIndexRange)rangeOfVisibleIndexesForContentOffset:(CGFloat)offset andHeight:(CGFloat)height{
float pixelMargin = rowHeight * 2;
BOOL first = NO;

CHGridIndexRange indexRange = {CHGridIndexPathMake(0, 0),CHGridIndexPathMake(0, 0)};

for(NSMutableArray *sectionArray in _index){
Expand All @@ -185,8 +186,6 @@ - (CHGridIndexRange)rangeOfVisibleIndexesForContentOffset:(CGFloat)offset andHei
}
}

first = NO;

return indexRange;
}

Expand Down
28 changes: 10 additions & 18 deletions CHGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ - (void)loadVisibleSectionTitlesForSectionRange:(CHSectionRange)range;
- (void)loadVisibleTilesForIndexPathRange:(CHGridIndexRange)range;
- (void)loadVisibleTileForIndexPath:(CHGridIndexPath)indexPath;
- (void)reuseHiddenTiles;
- (void)reuseTile:(CHTileView *)tile;
- (void)removeSectionTitleNotInRange:(CHSectionRange)range;
- (void)removeAllSubviews;
- (NSMutableArray *)tilesForSection:(int)section;
Expand Down Expand Up @@ -178,43 +177,38 @@ - (void)loadVisibleTileForIndexPath:(CHGridIndexPath)indexPath{
}

- (void)reuseHiddenTiles{
NSMutableArray *toReuse = [NSMutableArray array];
NSMutableArray *toReuse = [[NSMutableArray alloc] init];

CGRect b = self.bounds;
CGFloat contentOffsetY = self.contentOffset.y;
float pixelMargin = rowHeight * 2;
int maxReusable = [self maxNumberOfReusableTiles];

for(CHTileView *tile in visibleTiles){
if(tile.frame.origin.y + tile.frame.size.height < (contentOffsetY - pixelMargin) || tile.frame.origin.y > (contentOffsetY + b.size.height + pixelMargin)){
[toReuse addObject:tile];
if(reusableTiles.count < maxReusable) [reusableTiles addObject:tile];
}
}

for(CHTileView *tile in toReuse){
[self reuseTile:tile];
}
}

- (void)reuseTile:(CHTileView *)tile{
if(reusableTiles.count < [self maxNumberOfReusableTiles]) [reusableTiles addObject:tile];
[toReuse makeObjectsPerformSelector:@selector(removeFromSuperview)];
[visibleTiles removeObjectsInArray:toReuse];

[tile removeFromSuperview];
[visibleTiles removeObject:tile];
[toReuse release];
}

- (void)removeSectionTitleNotInRange:(CHSectionRange)range{
NSMutableArray *toDelete = [NSMutableArray array];
NSMutableArray *toDelete = [[NSMutableArray alloc] init];

for (CHSectionTitleView *title in visibleSectionTitles) {
if(title.section < range.start || title.section > range.end){
[toDelete addObject:title];
}
}

for(CHSectionTitleView *title in toDelete){
[title removeFromSuperview];
[visibleSectionTitles removeObject:title];
}
[toDelete makeObjectsPerformSelector:@selector(removeFromSuperview)];
[visibleSectionTitles removeObjectsInArray:toDelete];
[toDelete release];
}

- (void)reloadData{
Expand Down Expand Up @@ -439,8 +433,6 @@ - (void)calculateSectionTitleOffset{
if((offset + sectionTwo.frame.size.height) >= sectionTwo.yCoordinate){
f.origin.y = sectionTwo.yCoordinate - sectionTwo.frame.size.height;
}

//[self bringSubviewToFront:sectionTwo];
}
}else{
f.origin.y = title.yCoordinate;
Expand Down
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ If you disable scrolling with `setScrollingEnabled`, you can probably use this a

###Roadmap (roughly in order):

- Match speed and performance found in iPhone's Photos app grid view
- Match or exceed speed and performance found in iPhone's Photos app grid view
- More UITableView cloning, like scrolling to a tile at a specific indexPath
- Tile labels
- Flexible per-line setting (maybe a range?)
Expand Down

0 comments on commit 1d1d2cb

Please sign in to comment.