Permalink
Browse files

Adds in additional optional textfield methods that can be implemented…

…. Implements a better textString computation in the shouldChangeCharactersInRangeMethod. Updates example code to show a proper way of getting the cell's indexPath.

Using the saved indexPath will result in crashes, if the tableview is doing any inserts, deletes or moves.
  • Loading branch information...
1 parent 5e0c9c3 commit 38fccc1e4d054779082b5863fde35552eeb17117 James Van Metre committed Dec 7, 2012
Showing with 164 additions and 101 deletions.
  1. +26 −10 Classes/ELCTextfieldCell.h
  2. +119 −77 Classes/ELCTextfieldCell.m
  3. +15 −10 Classes/RootViewController.m
  4. +4 −4 ELCTextFieldCellDemo.xcodeproj/project.pbxproj
View
36 Classes/ELCTextfieldCell.h
@@ -1,27 +1,43 @@
//
-// ELCTextfieldCell.h
-// MobileWorkforce
+// ELCTextFieldCell.h
+// ELC Utility
//
-// Created by Collin Ruffenach on 10/22/10.
-// Copyright 2010 ELC Tech. All rights reserved.
+// Copyright 2012 ELC Tech. All rights reserved.
//
#import <UIKit/UIKit.h>
-@class ELCTextfieldCell;
+@class ELCTextFieldCell;
+
+@interface ELCInsetTextField : UITextField
+
+@property (nonatomic, assign) UIEdgeInsets inset;
+
+@end
@protocol ELCTextFieldDelegate <NSObject>
+
@optional
--(BOOL)textField:(ELCTextfieldCell *)inCell shouldReturnForIndexPath:(NSIndexPath*)inIndexPath withValue:(NSString *)inValue;
--(void)textField:(ELCTextfieldCell *)inCell didReturnWithIndexPath:(NSIndexPath*)inIndexPath withValue:(NSString *)inValue;
--(void)textField:(ELCTextfieldCell *)inCell updateTextLabelAtIndexPath:(NSIndexPath *)inIndexPath string:(NSString *)inValue;
+- (BOOL)textFieldCell:(ELCTextFieldCell *)inCell shouldReturnForIndexPath:(NSIndexPath*)inIndexPath withValue:(NSString *)inValue;
+- (void)textFieldCell:(ELCTextFieldCell *)inCell updateTextLabelAtIndexPath:(NSIndexPath *)inIndexPath string:(NSString *)inValue;
+
+- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
+- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
+- (void)textFieldDidBeginEditing:(UITextField *)textField;
+- (void)textFieldDidEndEditing:(UITextField *)textField;
+
@end
-@interface ELCTextfieldCell : UITableViewCell <UITextFieldDelegate>
+@interface ELCTextFieldCell : UITableViewCell <UITextFieldDelegate>
+
@property (nonatomic, assign) id<ELCTextFieldDelegate> delegate;
@property (nonatomic, retain) UILabel *leftLabel;
-@property (nonatomic, retain) UITextField *rightTextField;
+@property (nonatomic, retain) ELCInsetTextField *rightTextField;
@property (nonatomic, retain) NSIndexPath *indexPath;
@end
+@interface ELCInsetTextFieldCell : ELCTextFieldCell
+@property (nonatomic, assign) UIEdgeInsets inset;
+@end
+
View
196 Classes/ELCTextfieldCell.m
@@ -1,130 +1,172 @@
//
-// ELCTextfieldCell.m
-// MobileWorkforce
+// ELCTextFieldCell.m
+// ELC Utility
//
-// Created by Collin Ruffenach on 10/22/10.
-// Copyright 2010 ELC Tech. All rights reserved.
+// Copyright 2012 ELC Tech. All rights reserved.
//
-#import "ELCTextfieldCell.h"
+#import "ELCTextFieldCell.h"
+#pragma mark ELCInsetTextField
-@implementation ELCTextfieldCell
+@implementation ELCInsetTextField
-@synthesize delegate;
-@synthesize leftLabel;
-@synthesize rightTextField;
-@synthesize indexPath;
+- (CGRect)textRectForBounds:(CGRect)bounds
+{
+ return UIEdgeInsetsInsetRect(bounds, _inset);
+}
+- (CGRect)editingRectForBounds:(CGRect)bounds
+{
+ return UIEdgeInsetsInsetRect(bounds, _inset);
+}
+- (CGRect)placeholderRectForBounds:(CGRect)bounds
+{
+ return UIEdgeInsetsInsetRect(bounds, _inset);
+}
+
+@end
-- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
-
+#pragma mark - ELCTextFieldCell
+
+@implementation ELCTextFieldCell
+
+//using auto synthesizers
+
+- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
+{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
- leftLabel = [[UILabel alloc] initWithFrame:CGRectZero];
- [leftLabel setBackgroundColor:[UIColor clearColor]];
- [leftLabel setTextColor:[UIColor colorWithRed:.285 green:.376 blue:.541 alpha:1]];
- [leftLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];
- [leftLabel setTextAlignment:UITextAlignmentRight];
- [leftLabel setText:@"Left Field"];
- [self addSubview:leftLabel];
+ _leftLabel = [[UILabel alloc] initWithFrame:CGRectZero];
+ [_leftLabel setBackgroundColor:[UIColor clearColor]];
+ [_leftLabel setTextColor:[UIColor colorWithRed:.285 green:.376 blue:.541 alpha:1]];
+ [_leftLabel setFont:[UIFont fontWithName:@"Helvetica" size:17]];
+ [_leftLabel setTextAlignment:UITextAlignmentRight];
+ [self addSubview:_leftLabel];
- rightTextField = [[UITextField alloc] initWithFrame:CGRectZero];
- rightTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
- [rightTextField setDelegate:self];
- [rightTextField setPlaceholder:@"Right Field"];
- [rightTextField setFont:[UIFont systemFontOfSize:17]];
+ _rightTextField = [[ELCInsetTextField alloc] initWithFrame:CGRectZero];
+ _rightTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
+ [_rightTextField setDelegate:self];
+ [_rightTextField setFont:[UIFont systemFontOfSize:17]];
- // FOR MWF USE DONE
- [rightTextField setReturnKeyType:UIReturnKeyDone];
+ //Use Done for all of them.
+ [_rightTextField setReturnKeyType:UIReturnKeyDone];
- [self addSubview:rightTextField];
+ [self addSubview:_rightTextField];
}
return self;
}
//Layout our fields in case of a layoutchange (fix for iPad doing strange things with margins if width is > 400)
-- (void)layoutSubviews {
+- (void)layoutSubviews
+{
[super layoutSubviews];
CGRect origFrame = self.contentView.frame;
- if (leftLabel.text != nil) {
- leftLabel.frame = CGRectMake(origFrame.origin.x, origFrame.origin.y, 90, origFrame.size.height-1);
- rightTextField.frame = CGRectMake(origFrame.origin.x+105, origFrame.origin.y, origFrame.size.width-120, origFrame.size.height-1);
+ if (_leftLabel.text != nil) {
+ _leftLabel.hidden = NO;
+ _leftLabel.frame = CGRectMake(origFrame.origin.x, origFrame.origin.y, 125, origFrame.size.height-1);
+ _rightTextField.frame = CGRectMake(origFrame.origin.x+130, origFrame.origin.y, origFrame.size.width-140, origFrame.size.height);
} else {
- leftLabel.hidden = YES;
+ _leftLabel.hidden = YES;
NSInteger imageWidth = 0;
if (self.imageView.image != nil) {
imageWidth = self.imageView.image.size.width + 5;
}
- rightTextField.frame = CGRectMake(origFrame.origin.x+imageWidth+10, origFrame.origin.y, origFrame.size.width-imageWidth-20, origFrame.size.height-1);
+ _rightTextField.frame = CGRectMake(origFrame.origin.x+imageWidth+10, origFrame.origin.y, origFrame.size.width-imageWidth-20, origFrame.size.height-1);
}
+ [self setNeedsDisplay];
}
-- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
-
- [super setSelected:selected animated:animated];
-}
-
-- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
+- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
- return YES;
+ [super setSelected:selected animated:animated];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
BOOL ret = YES;
- if([delegate respondsToSelector:@selector(textField:shouldReturnForIndexPath:withValue:)])
- {
- ret = [delegate textField:self shouldReturnForIndexPath:indexPath withValue:self.rightTextField.text];
+ if([_delegate respondsToSelector:@selector(textFieldCell:shouldReturnForIndexPath:withValue:)]) {
+ ret = [_delegate textFieldCell:self shouldReturnForIndexPath:_indexPath withValue:self.rightTextField.text];
}
- if(ret)
+ if(ret) {
[textField resignFirstResponder];
+ }
return ret;
}
-- (void)textFieldDidEndEditing:(UITextField *)textField
+- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
- if([delegate respondsToSelector:@selector(textField:didReturnWithIndexPath:withValue:)])
- [delegate textField:self didReturnWithIndexPath:indexPath withValue:self.rightTextField.text];
-}
-
-
-- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
-
NSString *textString = self.rightTextField.text;
+ textString = [textString stringByReplacingCharactersInRange:range withString:string];
- if (range.length > 0) {
-
- textString = [textString stringByReplacingCharactersInRange:range withString:@""];
- }
+ if([_delegate respondsToSelector:@selector(textFieldCell:updateTextLabelAtIndexPath:string:)]) {
+ [_delegate textFieldCell:self updateTextLabelAtIndexPath:_indexPath string:textString];
+ }
- else {
-
- if(range.location == [textString length]) {
-
- textString = [textString stringByAppendingString:string];
- }
+ return YES;
+}
- else {
-
- textString = [textString stringByReplacingCharactersInRange:range withString:string];
- }
+- (BOOL)textFieldShouldClear:(UITextField *)textField
+{
+ if([_delegate respondsToSelector:@selector(updateTextLabelAtIndexPath:string:)]) {
+ [_delegate performSelector:@selector(updateTextLabelAtIndexPath:string:) withObject:_indexPath withObject:nil];
}
-
- if([delegate respondsToSelector:@selector(textField:updateTextLabelAtIndexPath:string:)])
- {
- [delegate textField:self updateTextLabelAtIndexPath:indexPath string:textString];
+ return YES;
+}
+
+- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
+{
+ if([_delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
+ return [_delegate textFieldShouldBeginEditing:(UITextField *)textField];
}
-
- return YES;
+ return YES;
+}
+
+- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
+{
+ if([_delegate respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
+ return [_delegate textFieldShouldEndEditing:(UITextField *)textField];
+ }
+ return YES;
+}
+
+- (void)textFieldDidBeginEditing:(UITextField *)textField
+{
+ if([_delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
+ return [_delegate textFieldDidBeginEditing:(UITextField *)textField];
+ }
+}
+
+- (void)textFieldDidEndEditing:(UITextField *)textField
+{
+ if([_delegate respondsToSelector:@selector(textFieldDidEndEditing:)]) {
+ [_delegate textFieldDidEndEditing:(UITextField*)textField];
+ }
}
-- (void)dealloc {
- [rightTextField resignFirstResponder];
- [leftLabel release];
- [rightTextField release];
- [indexPath release];
+- (void)dealloc
+{
+ _delegate = nil;
+ [_rightTextField resignFirstResponder];
+ [_leftLabel release];
+ [_rightTextField release];
+ [_indexPath release];
[super dealloc];
}
@end
+
+#pragma mark - ELCInsetTextFieldCell
+
+@implementation ELCInsetTextFieldCell
+- (void)setFrame:(CGRect)frame
+{
+ [super setFrame:UIEdgeInsetsInsetRect(frame, _inset)];
+}
+
+- (void)layoutSubviews
+{
+ [super layoutSubviews];
+ self.rightTextField.frame = CGRectInset(self.rightTextField.frame, 0, 4);
+}
+@end
View
25 Classes/RootViewController.m
@@ -69,7 +69,7 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
#pragma mark -
#pragma mark Table view data source
-- (void)configureCell:(ELCTextfieldCell *)cell atIndexPath:(NSIndexPath *)indexPath {
+- (void)configureCell:(ELCTextFieldCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.leftLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.rightTextField.placeholder = [self.placeholders objectAtIndex:indexPath.row];
@@ -102,9 +102,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
static NSString *CellIdentifier = @"Cell";
- ELCTextfieldCell *cell = (ELCTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+ ELCTextFieldCell *cell = (ELCTextFieldCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
- cell = [[[ELCTextfieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
+ cell = [[[ELCTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
@@ -154,17 +154,22 @@ - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)
#pragma mark ELCTextFieldCellDelegate Methods
--(void)textFieldDidReturnWithIndexPath:(NSIndexPath*)indexPath {
-
- if(indexPath.row < [labels count]-1) {
+- (void)textFieldDidEndEditing:(UITextField *)textField
+{
+ ELCTextFieldCell *textFieldCell = (ELCTextFieldCell*)textField.superview;
+ if (![textFieldCell isKindOfClass:ELCTextFieldCell.class]) {
+ return;
+ }
+ //It's a better method to get the indexPath like this, in case you are rearranging / removing / adding rows,
+ //the set indexPath wouldn't change
+ NSIndexPath *indexPath = [self.tableView indexPathForCell:textFieldCell];
+ if(indexPath != nil && indexPath.row < [labels count]-1) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
- [[(ELCTextfieldCell*)[self.tableView cellForRowAtIndexPath:path] rightTextField] becomeFirstResponder];
+ [[(ELCTextFieldCell*)[self.tableView cellForRowAtIndexPath:path] rightTextField] becomeFirstResponder];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
-
else {
-
- [[(ELCTextfieldCell*)[self.tableView cellForRowAtIndexPath:indexPath] rightTextField] resignFirstResponder];
+ [[(ELCTextFieldCell*)[self.tableView cellForRowAtIndexPath:indexPath] rightTextField] resignFirstResponder];
}
}
View
8 ELCTextFieldCellDemo.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 1A530DD01672B359008DEBE3 /* ELCTextfieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A530DCF1672B359008DEBE3 /* ELCTextfieldCell.m */; };
1D3623260D0F684500981E51 /* ELCTextFieldCellDemoAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D3623250D0F684500981E51 /* ELCTextFieldCellDemoAppDelegate.m */; };
1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; };
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
@@ -15,10 +16,10 @@
28AD73600D9D9599002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD735F0D9D9599002E5188 /* MainWindow.xib */; };
28C286E10D94DF7D0034E888 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28C286E00D94DF7D0034E888 /* RootViewController.m */; };
28F335F11007B36200424DE2 /* RootViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28F335F01007B36200424DE2 /* RootViewController.xib */; };
- E26407D712D3A5AF005DA328 /* ELCTextfieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E26407D612D3A5AF005DA328 /* ELCTextfieldCell.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 1A530DCF1672B359008DEBE3 /* ELCTextfieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ELCTextfieldCell.m; sourceTree = "<group>"; };
1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1D3623240D0F684500981E51 /* ELCTextFieldCellDemoAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ELCTextFieldCellDemoAppDelegate.h; sourceTree = "<group>"; };
1D3623250D0F684500981E51 /* ELCTextFieldCellDemoAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ELCTextFieldCellDemoAppDelegate.m; sourceTree = "<group>"; };
@@ -33,7 +34,6 @@
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* ELCTextFieldCellDemo-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ELCTextFieldCellDemo-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
E26407D512D3A5AF005DA328 /* ELCTextfieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ELCTextfieldCell.h; sourceTree = "<group>"; };
- E26407D612D3A5AF005DA328 /* ELCTextfieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ELCTextfieldCell.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -54,7 +54,7 @@
isa = PBXGroup;
children = (
E26407D512D3A5AF005DA328 /* ELCTextfieldCell.h */,
- E26407D612D3A5AF005DA328 /* ELCTextfieldCell.m */,
+ 1A530DCF1672B359008DEBE3 /* ELCTextfieldCell.m */,
28C286DF0D94DF7D0034E888 /* RootViewController.h */,
28C286E00D94DF7D0034E888 /* RootViewController.m */,
1D3623240D0F684500981E51 /* ELCTextFieldCellDemoAppDelegate.h */,
@@ -177,7 +177,7 @@
1D60589B0D05DD56006BFB54 /* main.m in Sources */,
1D3623260D0F684500981E51 /* ELCTextFieldCellDemoAppDelegate.m in Sources */,
28C286E10D94DF7D0034E888 /* RootViewController.m in Sources */,
- E26407D712D3A5AF005DA328 /* ELCTextfieldCell.m in Sources */,
+ 1A530DD01672B359008DEBE3 /* ELCTextfieldCell.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit 38fccc1

Please sign in to comment.