Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 169 lines (127 sloc) 5.428 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
//
// WCGridView.m
// StandingHere
//
// Created by Wess Cope on 5/3/12.
// Copyright (c) 2012 Wess Cope. All rights reserved.
//

#import "WCGridView.h"

static const CGFloat kGridViewDefaultRowHeight = 50.0f;

@interface WCGridView()
{
    NSUInteger _numberOfRows;
    NSMutableSet *_cellCache;
    NSMutableDictionary *_cellPaths;
}
@property (strong, nonatomic) UIScrollView *scrollView;

- (void)clearCachedCells;
@end

@implementation WCGridView
@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;
@synthesize scrollView = _scrollView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        _numberOfRows = 0;
        _cellPaths = [[NSMutableDictionary alloc] init];
        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
        
        [self addSubview:_scrollView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearCachedCells) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)reloadData
{
    [self layoutSubviews];
}

#pragma mark - Cell Caching
- (WCGridViewCell *)dequeueReusableCellWithIdentifier:(NSString *)reuseIdentifier
{
    NSSet *filteredCells = [_cellCache filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"reuseIdentifier == %@", reuseIdentifier]];
    WCGridViewCell *cell = [filteredCells anyObject];
    
    if(cell != nil)
        [_cellCache removeObject:cell];

    return cell;
}

- (void)clearCachedCells
{
    [_cellCache removeAllObjects];
}

#pragma mark - Cell Setup and Layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    static NSString *RowIdentifier = @"__WCGridViewRowIdentifier__";
    
    // Hack fix for rotation
    for(UIView *view in _scrollView.subviews)
        [view removeFromSuperview];

    
    _scrollView.frame = self.bounds;

    _numberOfRows = [self.dataSource numberOfRowsForGridView:self];
    
    int currentY = 0;
    int contentHeight = 0;

    CGFloat rowHeight = kGridViewDefaultRowHeight;
    int colCount = 0;
    for(int row = 0; row < _numberOfRows; row++)
    {
        if([self.dataSource respondsToSelector:@selector(gridView:heightForRowAtIndex:)])
            rowHeight = [self.dataSource gridView:self heightForRowAtIndex:row];
            
        CGRect currentRowFrame = CGRectMake(self.frame.origin.x, currentY, self.frame.size.width, rowHeight);
        UIView *rowView = (UIView *)[self dequeueReusableCellWithIdentifier:RowIdentifier];
        
        if(rowView == nil)
            rowView = [[UIView alloc] initWithFrame:currentRowFrame];
        else
            rowView.frame = currentRowFrame;
        
        NSInteger cols = [self.dataSource gridView:self numberOfColumnsForRowAtIndex:row];
        CGFloat cellWidth = (self.frame.size.width / cols);
        
        for(int column = 0; column < cols; column++)
        {
            WCGridIndexPath indexPath;
            indexPath.row = row;
            indexPath.column = column;
            indexPath.index = colCount++;
            
            WCGridViewCell *cell = [self.dataSource gridView:self cellForGridIndexPath:indexPath];
            cell.frame = CGRectMake((cellWidth * column), 0.0f, cellWidth, rowHeight);
            cell.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |
                                    UIViewAutoresizingFlexibleTopMargin |
                                    UIViewAutoresizingFlexibleHeight |
                                    UIViewAutoresizingFlexibleLeftMargin |
                                    UIViewAutoresizingFlexibleRightMargin |
                                    UIViewAutoresizingFlexibleWidth;
            
            [rowView addSubview:cell];
            
            if(cell.reuseIdentifier)
                [_cellCache addObject:cell];
            
            [_cellPaths setObject:[NSValue value:&indexPath withObjCType:@encode(WCGridIndexPath)] forKey:[NSNumber numberWithInt:(int)cell]];
            
            if([self.delegate respondsToSelector:@selector(gridView:didSelectCellAtIndexPath:)])
            {
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithCallback:^(id sender) {
                    [self.delegate gridView:self didSelectCellAtIndexPath:indexPath];
                }];
                
                cell.gestureRecognizers = [NSArray arrayWithObjects:tap, nil];
            }
        }
        
        CGRect rowFrame = rowView.frame;
        rowFrame.origin.y = (rowHeight * row);
        rowView.frame = rowFrame;
        contentHeight += rowView.frame.size.height;
        
        CGSize contentSize = _scrollView.contentSize;
        contentSize.height = contentHeight;
        
        _scrollView.contentSize = contentSize;
        
        [_scrollView addSubview:rowView];
    }
    
}


#pragma mark - Utlity Methods
- (WCGridIndexPath)indexPathForCell:(WCGridViewCell *)cell
{
    NSValue *indexPathValue = [_cellPaths objectForKey:[NSNumber numberWithInt:(int)cell]];
    WCGridIndexPath indexPath;
    [indexPathValue getValue:&indexPath];
    
    return indexPath;
}

@end
Something went wrong with that request. Please try again.