Created
March 26, 2010 02:49
-
-
Save jon/344424 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BPIdentityTableViewCell.h | |
// | |
// Created by Jon Olson on 12/06/09. | |
// Copyright 2010 Ballistic Pigeon, LLC. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface BPIdentityTableViewCell : UITableViewCell { | |
id delegate; | |
id identifiedObject; | |
UIImage *editingPlaceholderImage; | |
UIImage *placeholderImage; | |
UIImage *objectImage; | |
UIView *backgroundImageView; | |
UIView *selectedBackgroundImageView; | |
UIButton *imageButton; | |
} | |
@property (nonatomic, assign) id delegate; | |
@property (nonatomic, retain) id identifiedObject; | |
@property (nonatomic, retain) id editingPlaceholderImage; | |
@property (nonatomic, retain) id placeholderImage; | |
@property (nonatomic, retain) id objectImage; | |
- (void)refresh; | |
- (void)refresh:(BOOL)animated; | |
- (CGRect)imageFrame; | |
@end | |
@interface NSObject (BPIdentityTableViewCellDelegate) | |
- (void)identityCellEditImage:(BPIdentityTableViewCell *)cell; | |
- (void)identityCell:(BPIdentityTableViewCell *)cell zoomImage:(UIImage *)image; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BPIdentityTableViewCell.m | |
// | |
// Created by Jon Olson on 12/06/09. | |
// Copyright 2010 Ballistic Pigeon, LLC. All rights reserved. | |
// | |
#import "BPIdentityTableViewCell.h" | |
#import "UIImage+Resize.h" | |
#define WIDTH 320.0 | |
#define HEIGHT 60.0 | |
#define CONTENT_GAP 5.0 | |
#define CONTENT_MARGIN 10.0 | |
#define OFFSET (HEIGHT+CONTENT_GAP) | |
@implementation BPIdentityTableViewCell | |
#pragma mark - | |
#pragma mark Initialization and deallocation | |
- (id)init { | |
const CGRect frame = CGRectMake(0, 0, WIDTH, HEIGHT); | |
if (self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]) { | |
self.frame = frame; | |
self.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
self.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; | |
self.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; | |
imageButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; | |
imageButton.frame = CGRectZero; | |
imageButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; | |
imageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; | |
[imageButton addTarget:self action:@selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside]; | |
[self.contentView addSubview:imageButton]; | |
self.placeholderImage = [UIImage imageNamed:@"image.png"]; | |
self.editingPlaceholderImage = [UIImage imageNamed:@"imageEdit.png"]; | |
} | |
return self; | |
} | |
- (void)dealloc { | |
[identifiedObject release]; | |
[placeholderImage release]; | |
[objectImage release]; | |
[imageButton release]; | |
[super dealloc]; | |
} | |
#pragma mark - | |
#pragma mark Accessors | |
@synthesize delegate; | |
@synthesize identifiedObject; | |
@synthesize editingPlaceholderImage; | |
@synthesize placeholderImage; | |
@synthesize objectImage; | |
- (void)setIdetifiedObject:(id)anObject { | |
if (identifiedObject != anObject) { | |
[identifiedObject release]; | |
identifiedObject = [anObject retain]; | |
[self refresh:YES]; | |
} | |
} | |
- (void)updateImage { | |
UIImage *image = nil; | |
if (objectImage) | |
image = objectImage; | |
else { | |
if (self.editing) | |
image = editingPlaceholderImage; | |
else | |
image = placeholderImage; | |
} | |
[imageButton setImage:[image resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:imageButton.frame.size interpolationQuality:kCGInterpolationDefault] forState:UIControlStateNormal]; | |
} | |
- (void)setEditingPlaceholderImage:(id)anImage { | |
if (editingPlaceholderImage != anImage) { | |
[editingPlaceholderImage release]; | |
editingPlaceholderImage = [anImage retain]; | |
[self updateImage]; | |
} | |
} | |
- (void)setObjectImage:(id)anImage { | |
if (objectImage != anImage) { | |
[objectImage release]; | |
objectImage = [anImage retain]; | |
[self updateImage]; | |
} | |
} | |
- (void)setPlaceholderImage:(id)anImage { | |
if (placeholderImage != anImage) { | |
[placeholderImage release]; | |
placeholderImage = [anImage retain]; | |
[self updateImage]; | |
} | |
} | |
#pragma mark - | |
#pragma mark Handling proper layout | |
static void Realign(UIView *view) { | |
CGRect viewFrame = view.frame; | |
viewFrame.origin.x += OFFSET; | |
viewFrame.size.width -= OFFSET; | |
view.frame = viewFrame; | |
} | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
CGRect contentBounds = self.contentView.bounds; | |
CGRect accessoryFrame = self.accessoryView.frame; | |
imageButton.frame = CGRectMake(0, 0, contentBounds.size.height, contentBounds.size.height); | |
const CGFloat x = contentBounds.size.height + CONTENT_MARGIN; | |
self.textLabel.frame = CGRectMake(x, self.textLabel.frame.origin.y, contentBounds.size.width - x - accessoryFrame.size.width, self.textLabel.frame.size.height); | |
self.detailTextLabel.frame = CGRectMake(x, self.detailTextLabel.frame.origin.y, contentBounds.size.width - x - accessoryFrame.size.width, self.detailTextLabel.frame.size.height); | |
self.textLabel.backgroundColor = self.detailTextLabel.backgroundColor = [UIColor clearColor]; | |
self.textLabel.shadowOffset = self.detailTextLabel.shadowOffset = CGSizeMake(0, 1); | |
if (self.editing) { | |
self.textLabel.shadowColor = nil; | |
self.textLabel.textColor = [UIColor blackColor]; | |
self.detailTextLabel.shadowColor = nil; | |
self.detailTextLabel.textColor = [UIColor grayColor]; | |
const CGRect backgroundFrame = self.backgroundView.frame; | |
selectedBackgroundImageView.frame = backgroundImageView.frame = CGRectMake(OFFSET, 0, backgroundFrame.size.width - OFFSET, backgroundFrame.size.height); | |
} | |
else { | |
self.textLabel.textColor = self.detailTextLabel.textColor = [UIColor colorWithRed:0.122 green:0.157 blue:0.224 alpha:1.0]; | |
self.textLabel.shadowColor = self.detailTextLabel.shadowColor = [UIColor whiteColor]; | |
} | |
} | |
#pragma mark - | |
#pragma mark Editing | |
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { | |
if (editing) { | |
const CGRect backgroundFrame = self.backgroundView.frame; | |
CGRect frame = CGRectMake(OFFSET, 0, backgroundFrame.size.width - OFFSET, backgroundFrame.size.height); | |
UIImage *background = [UIImage imageNamed:@"TableCellBackground.png"]; | |
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:frame]; | |
backgroundView.image = [background stretchableImageWithLeftCapWidth:11 topCapHeight:11]; | |
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth; | |
backgroundView.alpha = 0.0; | |
[self.backgroundView addSubview:backgroundView]; | |
[backgroundView release]; | |
backgroundImageView = backgroundView; | |
UIImage *selectedBackground = [UIImage imageNamed:@"SelectedTableCellBackground.png"]; | |
UIImageView *selectedBackgroundView = [[UIImageView alloc] initWithFrame:frame]; | |
selectedBackgroundView.image = [selectedBackground stretchableImageWithLeftCapWidth:11 topCapHeight:0]; | |
selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; | |
selectedBackgroundView.alpha = 0.0; | |
[self.selectedBackgroundView addSubview:selectedBackgroundView]; | |
[selectedBackgroundView release]; | |
selectedBackgroundImageView = selectedBackgroundView; | |
imageButton.userInteractionEnabled = YES; | |
if (animated) | |
[UIView beginAnimations:@"fadeInBackground" context:nil]; | |
backgroundView.alpha = 1.0; | |
selectedBackgroundView.alpha = 1.0; | |
[super setEditing:YES animated:animated]; | |
if (animated) | |
[UIView commitAnimations]; | |
} | |
else { | |
[self.backgroundView removeAllSubviews]; | |
[self.selectedBackgroundView removeAllSubviews]; | |
selectedBackgroundImageView = nil; | |
backgroundImageView = nil; | |
imageButton.userInteractionEnabled = !!objectImage; | |
[super setEditing:NO animated:animated]; | |
} | |
[self updateImage]; | |
} | |
- (UITableViewCellEditingStyle)editingStyle { | |
return UITableViewCellEditingStyleNone; | |
} | |
#pragma mark - | |
#pragma mark Editing image | |
- (void)imageTapped:(id)sender { | |
if (self.editing) { | |
if ([delegate respondsToSelector:@selector(identityCellEditImage:)]) | |
[delegate identityCellEditImage:self]; | |
} | |
else { | |
if ([delegate respondsToSelector:@selector(identityCell:zoomImage:)]) | |
[delegate identityCell:self zoomImage:objectImage]; | |
} | |
} | |
- (CGRect)imageFrame { | |
return [self convertRect:[self.contentView convertRect:imageButton.frame toView:self] toView:self.superview]; | |
} | |
#pragma mark - | |
#pragma mark Indentation | |
- (BOOL)shouldIndentWhileEditing { | |
return NO; | |
} | |
#pragma mark - | |
#pragma mark Refreshing display | |
- (void)refresh { | |
// Do nothing | |
} | |
- (void)refresh:(BOOL)animated { | |
if (animated) | |
[UIView beginAnimations:@"refreshIdentity" context:nil]; | |
[self refresh]; | |
if (animated) | |
[UIView commitAnimations]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment