Skip to content

Commit

Permalink
Refactor to a reusable base cell and a default cell implementation. T…
Browse files Browse the repository at this point in the history
…his makes it easy to create custom sub classes.
  • Loading branch information
René Sprotte committed Apr 10, 2011
1 parent 6215039 commit d0ffb3a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 58 deletions.
8 changes: 8 additions & 0 deletions MMGridView.xcodeproj/project.pbxproj
Expand Up @@ -22,6 +22,8 @@
7E6EF3DE1340B52E00DD91EC /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E6EF3DD1340B52E00DD91EC /* RootViewController.m */; };
7E6EF3E11340B52E00DD91EC /* RootViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7E6EF3DF1340B52E00DD91EC /* RootViewController.xib */; };
7E6EF3EB1340BE9300DD91EC /* libMMGridView.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7E6EF3781340A48600DD91EC /* libMMGridView.a */; };
7E79DC22135240430085C23D /* MMGridViewDefaultCell.h in Headers */ = {isa = PBXBuildFile; fileRef = 7E79DC20135240430085C23D /* MMGridViewDefaultCell.h */; };
7E79DC23135240430085C23D /* MMGridViewDefaultCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E79DC21135240430085C23D /* MMGridViewDefaultCell.m */; };
7EBF307E1349F161007AE714 /* MMGridViewCell.h in Headers */ = {isa = PBXBuildFile; fileRef = 7EBF307C1349F161007AE714 /* MMGridViewCell.h */; };
7EBF307F1349F161007AE714 /* MMGridViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EBF307D1349F161007AE714 /* MMGridViewCell.m */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -58,6 +60,8 @@
7E6EF3DC1340B52E00DD91EC /* RootViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = "<group>"; };
7E6EF3DD1340B52E00DD91EC /* RootViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
7E6EF3E01340B52E00DD91EC /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/RootViewController.xib; sourceTree = "<group>"; };
7E79DC20135240430085C23D /* MMGridViewDefaultCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MMGridViewDefaultCell.h; sourceTree = "<group>"; };
7E79DC21135240430085C23D /* MMGridViewDefaultCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MMGridViewDefaultCell.m; sourceTree = "<group>"; };
7EBF307C1349F161007AE714 /* MMGridViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MMGridViewCell.h; path = Classes/MMGridViewCell.h; sourceTree = "<group>"; };
7EBF307D1349F161007AE714 /* MMGridViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MMGridViewCell.m; path = Classes/MMGridViewCell.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -122,6 +126,8 @@
7E6EF3A81340A53100DD91EC /* MMGridView.m */,
7EBF307C1349F161007AE714 /* MMGridViewCell.h */,
7EBF307D1349F161007AE714 /* MMGridViewCell.m */,
7E79DC20135240430085C23D /* MMGridViewDefaultCell.h */,
7E79DC21135240430085C23D /* MMGridViewDefaultCell.m */,
);
path = MMGridView;
sourceTree = "<group>";
Expand Down Expand Up @@ -171,6 +177,7 @@
files = (
7E6EF3A91340A53100DD91EC /* MMGridView.h in Headers */,
7EBF307E1349F161007AE714 /* MMGridViewCell.h in Headers */,
7E79DC22135240430085C23D /* MMGridViewDefaultCell.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -259,6 +266,7 @@
files = (
7E6EF3AA1340A53100DD91EC /* MMGridView.m in Sources */,
7EBF307F1349F161007AE714 /* MMGridViewCell.m in Sources */,
7E79DC23135240430085C23D /* MMGridViewDefaultCell.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 0 additions & 6 deletions MMGridView/Classes/MMGridViewCell.h
Expand Up @@ -25,16 +25,10 @@

@interface MMGridViewCell : UIView
{
UILabel *textLabel;
UIView *textLabelBackgroundView;
UIView *backgroundView;
MMGridView *gridView;
NSInteger index;
}

@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) UIView *textLabelBackgroundView;
@property (nonatomic, retain) UIView *backgroundView;
@property (nonatomic, readonly) MMGridView *gridView;
@property (nonatomic, readonly) NSInteger index;

Expand Down
51 changes: 0 additions & 51 deletions MMGridView/Classes/MMGridViewCell.m
Expand Up @@ -29,75 +29,24 @@ @interface MMGridViewCell()

@implementation MMGridViewCell

@synthesize textLabel;
@synthesize textLabelBackgroundView;
@synthesize backgroundView;
@synthesize gridView;
@synthesize index;

- (void)dealloc
{
[textLabel release];
[textLabelBackgroundView release];
[backgroundView release];
[super dealloc];
}


- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// Background view
self.backgroundView = [[UIView alloc] initWithFrame:CGRectNull];
self.backgroundView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:self.backgroundView];

// Label
self.textLabelBackgroundView = [[UIView alloc] initWithFrame:CGRectNull];
self.textLabelBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];

self.textLabel = [[UILabel alloc] initWithFrame:CGRectNull];
self.textLabel.textAlignment = UITextAlignmentRight;
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.font = [UIFont systemFontOfSize:12];

[self.textLabelBackgroundView addSubview:self.textLabel];
[self addSubview:self.textLabelBackgroundView];
}

return self;
}


- (void)layoutSubviews
{
[super layoutSubviews];

int labelHeight = 30;
int inset = 5;

// Background view
self.backgroundView.frame = self.bounds;
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Layout label
self.textLabelBackgroundView.frame = CGRectMake(0,
self.bounds.size.height - labelHeight - inset,
self.bounds.size.width,
labelHeight);
self.textLabelBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Layout label background
CGRect f = CGRectMake(0,
0,
self.textLabel.superview.bounds.size.width,
self.textLabel.superview.bounds.size.height);
self.textLabel.frame = CGRectInset(f, inset, 0);
self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}


- (void)setIndex:(NSNumber *)theIndex
{
index = [theIndex intValue];
Expand Down
24 changes: 24 additions & 0 deletions MMGridView/MMGridViewDefaultCell.h
@@ -0,0 +1,24 @@
//
// MMGridViewDefaultCell.h
// MMGridView
//
// Created by René Sprotte on 10.04.11.
// Copyright 2011 metaminded. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MMGridViewCell.h"


@interface MMGridViewDefaultCell : MMGridViewCell
{
UILabel *textLabel;
UIView *textLabelBackgroundView;
UIView *backgroundView;
}

@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) UIView *textLabelBackgroundView;
@property (nonatomic, retain) UIView *backgroundView;

@end
80 changes: 80 additions & 0 deletions MMGridView/MMGridViewDefaultCell.m
@@ -0,0 +1,80 @@
//
// MMGridViewDefaultCell.m
// MMGridView
//
// Created by René Sprotte on 10.04.11.
// Copyright 2011 metaminded. All rights reserved.
//

#import "MMGridViewDefaultCell.h"


@implementation MMGridViewDefaultCell

@synthesize textLabel;
@synthesize textLabelBackgroundView;
@synthesize backgroundView;

- (void)dealloc
{
[textLabel release];
[textLabelBackgroundView release];
[backgroundView release];
[super dealloc];
}


- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// Background view
self.backgroundView = [[UIView alloc] initWithFrame:CGRectNull];
self.backgroundView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:self.backgroundView];

// Label
self.textLabelBackgroundView = [[UIView alloc] initWithFrame:CGRectNull];
self.textLabelBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];

self.textLabel = [[UILabel alloc] initWithFrame:CGRectNull];
self.textLabel.textAlignment = UITextAlignmentRight;
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.font = [UIFont systemFontOfSize:12];

[self.textLabelBackgroundView addSubview:self.textLabel];
[self addSubview:self.textLabelBackgroundView];
}

return self;
}


- (void)layoutSubviews
{
[super layoutSubviews];

int labelHeight = 30;
int inset = 5;

// Background view
self.backgroundView.frame = self.bounds;
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Layout label
self.textLabelBackgroundView.frame = CGRectMake(0,
self.bounds.size.height - labelHeight - inset,
self.bounds.size.width,
labelHeight);
self.textLabelBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Layout label background
CGRect f = CGRectMake(0,
0,
self.textLabel.superview.bounds.size.width,
self.textLabel.superview.bounds.size.height);
self.textLabel.frame = CGRectInset(f, inset, 0);
self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

@end
3 changes: 2 additions & 1 deletion MMGridViewDemo/RootViewController.m
Expand Up @@ -21,6 +21,7 @@

#import "RootViewController.h"
#import "AnyViewController.h"
#import "MMGridViewDefaultCell.h"

@implementation RootViewController

Expand Down Expand Up @@ -72,7 +73,7 @@ - (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView

- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index
{
MMGridViewCell *cell = [[[MMGridViewCell alloc] initWithFrame:CGRectNull] autorelease];
MMGridViewDefaultCell *cell = [[[MMGridViewDefaultCell alloc] initWithFrame:CGRectNull] autorelease];
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", index];
return cell;
}
Expand Down

0 comments on commit d0ffb3a

Please sign in to comment.