Skip to content

Instantly share code, notes, and snippets.

@jon
Created February 9, 2010 18:50
Show Gist options
  • Save jon/299512 to your computer and use it in GitHub Desktop.
Save jon/299512 to your computer and use it in GitHub Desktop.
A tappable button cell for UITableViews
//
// BPButtonTableViewCell.h
// Brewtool
//
// Created by Jon Olson on 9/24/09.
// Copyright 2009 Ballistic Pigeon, LLC. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum {
BPButtonTableViewCellStylePlain,
BPButtonTableViewCellStyleRed
} BPButtonTableViewCellStyle;
@interface BPButtonTableViewCell : UITableViewCell {
id target;
SEL action;
}
- (id)initWithStyle:(BPButtonTableViewCellStyle)cellStyle;
- (id)initWithStyle:(BPButtonTableViewCellStyle)cellStyle labelText:(NSString *)labelText target:(id)aTarget action:(SEL)anAction;
- (void)setTarget:(id)aTarget action:(SEL)anAction;
@end
//
// BPButtonTableViewCell.m
// Brewtool
//
// Created by Jon Olson on 9/24/09.
// Copyright 2009 Ballistic Pigeon, LLC. All rights reserved.
//
#import "BPButtonTableViewCell.h"
@implementation BPButtonTableViewCell
- (id)init {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]) {
// Prevent ugly blueness
self.selectionStyle = UITableViewCellSelectionStyleNone;
// Set text properties
self.textLabel.textAlignment = UITextAlignmentCenter;
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.textLabel.shadowOffset = CGSizeMake(0, -1);
self.textLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
}
return self;
}
- (id)initWithStyle:(BPButtonTableViewCellStyle)cellStyle {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]) {
// Prevent ugly blueness
self.selectionStyle = UITableViewCellSelectionStyleNone;
UIColor *textColor = nil;
UIFont *font = nil;
UIImage *background = nil;
BOOL shadow = NO;
switch (cellStyle) {
case BPButtonTableViewCellStylePlain:
textColor = [UIColor colorWithRed:0.22 green:0.33 blue:0.53 alpha:1];
font = [UIFont boldSystemFontOfSize:15.0];
shadow = NO;
break;
case BPButtonTableViewCellStyleRed:
textColor = [UIColor whiteColor];
font = [UIFont boldSystemFontOfSize:20.0];
background = [UIImage imageNamed:@"redButton.png"];
shadow = YES;
break;
}
// Set text properties
self.textLabel.textAlignment = UITextAlignmentCenter;
self.textLabel.textColor = textColor;
self.textLabel.font = font;
if (shadow) {
self.textLabel.shadowOffset = CGSizeMake(0, -1);
self.textLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
}
if (background) {
// Set button background image
UIImage *image = [background stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];
self.backgroundColor = [UIColor clearColor];
self.backgroundView = [[UIImageView alloc] initWithImage:image];
}
}
return self;
}
- (id)initWithStyle:(BPButtonTableViewCellStyle)cellStyle labelText:(NSString *)labelText target:(id)aTarget action:(SEL)anAction {
if (self = [self initWithStyle:cellStyle]) {
self.textLabel.text = labelText;
[self setTarget:aTarget action:anAction];
}
return self;
}
- (id)initWithLabelText:(NSString *)labelText target:(id)aTarget action:(SEL)anAction {
if (self = [self init]) {
self.textLabel.text = labelText;
[self setTarget:aTarget action:anAction];
}
return self;
}
#pragma mark -
#pragma mark Ensuring the cell always displays correctly
- (UITableViewCellEditingStyle)editingStyle {
return UITableViewCellEditingStyleNone;
}
- (BOOL)shouldIndentWhileEditing {
return NO;
}
#pragma mark -
#pragma mark Handling selection and touches (triggering)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected)
[target performSelector:action withObject:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] != 1)
return;
UITouch *touch = [touches anyObject]; // Singleton set
CGPoint location = [touch locationInView:self];
if (CGRectContainsPoint(self.bounds, location))
[target performSelector:action withObject:self];
}
- (void)setTarget:(id)aTarget action:(SEL)anAction {
if (target != aTarget) {
[target release];
target = [aTarget retain];
}
action = anAction;
}
- (void)dealloc {
[target release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment