Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 16, 2015 03:29
Show Gist options
  • Select an option

  • Save lamprosg/5370538 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5370538 to your computer and use it in GitHub Desktop.
(iOS) Custom Cell - Subclass of UITableViewCell
@interface RightDetailedCell : UITableViewCell
@property (nonatomic, assign) BOOL arrow;
@property (nonatomic, assign) BOOL animated;
@end
@implementation RightDetailedCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
//Cell must be subtitled
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
style = UITableViewCellStyleSubtitle;
//Cell not colored when selected
self.selectionStyle = UITableViewCellSelectionStyleNone;
//Set proper fonts
self.textLabel.font = CELLFONT;
self.detailTextLabel.font = CELLFONT;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
}
else {
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
//Text frame left aligned
if (self.animated) {
CGRect textFrame = CGRectMake(3*CELLPADDING,2, self.contentView.frame.size.width - CGRectGetWidth(self.detailTextLabel.frame), self.contentView.bounds.size.height);
self.textLabel.frame = textFrame;
}
else {
CGRect textFrame = CGRectMake(CELLPADDING,2, self.contentView.frame.size.width - CGRectGetWidth(self.detailTextLabel.frame), self.contentView.bounds.size.height);
self.textLabel.frame = textFrame;
}
//Animation
if (self.animated)
{
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = self.textLabel.frame;
frame.origin.x = CELLPADDING;
self.textLabel.frame = frame;
}];
}
//Subtitle frame right aligned
CGRect detailedTextFrame = CGRectMake(self.contentView.frame.size.width-CELLPADDING-CGRectGetWidth(self.detailTextLabel.frame), 0, CGRectGetWidth(self.detailTextLabel.frame) , self.contentView.bounds.size.height);
self.detailTextLabel.frame = detailedTextFrame;
//Set accessory indicator
if (self.arrow)
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
else
self.accessoryType = 0;
}
@end
#import <UIKit/UIKit.h>
@class StringInputTableViewCell;
@protocol StringInputTableViewCellDelegate <NSObject>
@optional
- (void)tableViewCell:(StringInputTableViewCell *)cell didEndEditingWithString:(NSString *)value;
@end
@interface StringInputTableViewCell : UITableViewCell <UITextFieldDelegate> {
UITextField *textField;
}
@property (nonatomic, strong) NSString *stringValue;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) NSString *key;
@property (nonatomic, readwrite) BOOL allowEnabledOnlyWhenEditing;
@property (weak) IBOutlet id<StringInputTableViewCellDelegate> delegate;
@end
#import "StringInputTableViewCell.h"
#import "UIConstants.h"
@implementation StringInputTableViewCell {
UITableViewCellStyle savedStyle;
}
@synthesize delegate;
@synthesize stringValue;
@synthesize textField;
@synthesize allowEnabledOnlyWhenEditing;
//Custom function
- (void)initalizeInputView {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textField = [[UITextField alloc] initWithFrame:CGRectZero];
self.textField.autocorrectionType = UITextAutocorrectionTypeDefault;
self.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.textField.textAlignment = NSTextAlignmentLeft;
self.textField.font = [UIFont systemFontOfSize:17.0f];
self.textField.clearButtonMode = UITextFieldViewModeNever;
self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.textField.textColor = kMainColor;
[self addSubview:self.textField];
self.accessoryType = UITableViewCellAccessoryNone;
self.textField.delegate = self;
self.allowEnabledOnlyWhenEditing = NO;
}
//Defauly cell initializer (overrided)
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
savedStyle = style;
[self initalizeInputView];
}
return self;
}
//Initializer (basically when initializing from xib or storyboard)
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initalizeInputView];
}
return self;
}
//Sets a boolean value that indicates whether the cell is selected
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
[self.textField becomeFirstResponder];
}
}
//Same as above with optional animation between states
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
[self.textField becomeFirstResponder];
}
}
//Custom
- (void)setStringValue:(NSString *)value {
self.textField.text = value;
}
//Custom
- (NSString *)stringValue {
return self.textField.text;
}
//The text field calls this method whenever the user taps the return button (UITextField.h)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.textField resignFirstResponder];
return YES;
}
//Tells the delegate that editing stopped for the specified text field (UITextField.h)
//called after the text field resigns its first responder
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (delegate && [delegate respondsToSelector:@selector(tableViewCell:didEndEditingWithString:)]) {
[delegate tableViewCell:self didEndEditingWithString:self.stringValue];
}
UITableView *tableView = (UITableView *)self.superview;
[tableView deselectRowAtIndexPath:[tableView indexPathForCell:self] animated:YES];
}
//Draws the subviews
- (void)layoutSubviews {
[super layoutSubviews];
CGRect editFrame = CGRectInset(self.contentView.frame, 10, 10);
if (savedStyle == UITableViewCellStyleValue2) {
self.textField.frame = CGRectMake(107, CGRectGetMinY(editFrame), CGRectGetWidth(editFrame)-107, CGRectGetHeight(editFrame));
} else {
if (self.imageView.image) {
CGSize imgSize = self.imageView.image.size;
editFrame.origin.x += imgSize.width + 10;
editFrame.size.width -= imgSize.width + 10;
}
if (self.textLabel.text && [self.textLabel.text length] != 0) {
CGSize textSize = [self.textLabel sizeThatFits:CGSizeZero];
editFrame.origin.x += textSize.width + 10;
editFrame.size.width -= textSize.width + 10;
self.textField.textAlignment = NSTextAlignmentRight;
} else {
self.textField.textAlignment = NSTextAlignmentLeft;
}
self.textField.frame = editFrame;
}
}
//Toggles the receiver into and out of editing mode
//Sets editing=YES or NO
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
if (self.allowEnabledOnlyWhenEditing) {
if (editing) {
self.textField.enabled = YES;
self.textLabel.textColor = kCellTextLabelColorEditing;
}
else {
self.textField.enabled = NO;
self.textLabel.textColor = kCellTextLabelColorNormal;
}
}
[super setEditing:editing animated:animated];
}
@end
#import <UIKit/UIKit.h>
@class SwitchCell;
@protocol SwitchCellDelegate <NSObject>
@optional
- (void)tableViewCell:(SwitchCell *)cell switchChangedTo:(BOOL) state; //The delegate function
@end
@interface SwitchCell : UITableViewCell
@property (nonatomic, strong) NSString *textString;
@property (nonatomic, assign) BOOL switchState;
@property (weak) id <SwitchCellDelegate> delegate;
@end
#import "SwitchCell.h"
#import "UIConstants.h"
@interface SwitchCell () {
UILabel *titleLabel;
UISwitch *switchCtl;
}
@end
@implementation SwitchCell
- (void)initializeView {
// Initialization code
//Cell os not selectable
self.selectionStyle = UITableViewCellSelectionStyleNone;
// layoutSubViews will decide the final frame
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
titleLabel.font = CELLFONT;
[self.contentView addSubview:titleLabel];
switchCtl = [[UISwitch alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:switchCtl];
self.accessoryType = UITableViewCellAccessoryNone;
//What will happen if the switch is changed
[switchCtl addTarget:self action:@selector(changeSwitchState) forControlEvents:UIControlEventValueChanged];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self initializeView];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
titleLabel.text = self.textString;
CGRect labelFrame = CGRectMake(CELLPADDING, 0, self.contentView.bounds.size.width- switchCtl.bounds.size.width-2*CELLPADDING, self.contentView.bounds.size.height);
titleLabel.frame = labelFrame;
CGRect switchFrame = CGRectMake(self.contentView.bounds.size.width - switchCtl.bounds.size.width - CELLPADDING, self.contentView.bounds.size.height/2 - switchCtl.bounds.size.height/2, switchCtl.bounds.size.width, switchCtl.bounds.size.height);
switchCtl.frame = switchFrame;
[switchCtl setOn:self.switchState animated:YES];
}
-(void) changeSwitchState
{
self.switchState = !self.switchState;
[switchCtl setOn:self.switchState animated:YES];
//Check and call the delegate function
if (self.delegate && [self.delegate respondsToSelector:@selector(tableViewCell:switchChangedTo:)]) {
[self.delegate tableViewCell:self switchChangedTo:self.switchState];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment