Skip to content

Instantly share code, notes, and snippets.

@jeksys
Created May 4, 2011 20:19
Show Gist options
  • Save jeksys/955948 to your computer and use it in GitHub Desktop.
Save jeksys/955948 to your computer and use it in GitHub Desktop.
EditableViewCell
//
// EditableViewCell.h
// Editable UITableViewCell with a UITextField as an edit tool
// Created by Eugene Yagrushkin on 11-05-04.
// Copyright 2011 Eugene Yagrushkin All rights reserved.
//
#import <UIKit/UIKit.h>
@interface EditableViewCell : UITableViewCell {
CGRect editRect;
UITextField *editField;
}
@property (nonatomic, readonly, retain) UITextField *editField;
- (void) SaveTitle;
@end
//
// EditableViewCell.m
//
// Created by Eugene Yagrushkin on 11-05-04.
// Copyright 2011 Eugene Yagrushkin All rights reserved.
//
#import "EditableViewCell.h"
@implementation EditableViewCell
@synthesize editField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
editRect = CGRectMake(83, 12, self.contentView.bounds.size.width-83, 19);
editField = [[UITextField alloc] initWithFrame:editRect];
editField.font = [UIFont boldSystemFontOfSize:15];
editField.textAlignment = UITextAlignmentLeft;
editField.textColor = [UIColor blackColor];
editField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:editField];
[self.contentView bringSubviewToFront:editField];
self.editField.enabled = NO;
self.editField.hidden = YES;
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
editRect = CGRectMake(self.textLabel.frame.origin.x, self.textLabel.frame.origin.y, self.contentView.frame.size.width-self.textLabel.frame.origin.x, self.textLabel.frame.size.height);
editField.frame = editRect;//CGRectMake(0, 0, 200, 30);
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateEditingMask) {
self.textLabel.hidden = YES;
self.editField.enabled = YES;
self.editField.hidden = NO;
self.editField.text = self.textLabel.text;
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (!(state & UITableViewCellStateEditingMask)) {
self.editField.enabled = NO;
self.editField.hidden = YES;
self.textLabel.hidden = NO;
self.textLabel.text = self.editField.text;
[self SaveTitle];
}
}
- (void)dealloc {
[editField release];
[super dealloc];
}
- (void) SaveTitle
{
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment