2013-04-15 126 views
0

的问题是,我的UITextField包装类是没有得到的UITextField到一个UITableViewCell(但是,常规的UITextField视图工作得很好)内出现:为什么我的UITextField Wrapper不能显示在UITableViewCell中?

enter image description here

这里是我的自定义的UITextField:

部首:

#import <UIKit/UIKit.h> 

@protocol TUTextFieldDelegate <NSObject> 

- (BOOL)textField:(UITextField *)textField 
shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string; 

- (BOOL)textFieldShouldClear:(UITextField *)textField; 

- (void)textFieldDidBeginEditing:(UITextField *)textField; 

- (void)textFieldDidEndEditing:(UITextField *)textField; 

@end 

@interface TUTextField : UIControl <UITextFieldDelegate> 

@property (nonatomic, assign) BOOL fixedDecimalPoint; 
@property (nonatomic, assign) id <TUTextFieldDelegate>delegate; 

@property (nonatomic, assign) UIKeyboardType keyboardType; 
@property (nonatomic, strong) NSString *placeholder; 
@property (nonatomic, assign) UITextBorderStyle borderStyle; 
@property (nonatomic, assign) BOOL adjustsFontSizeToFitWidth; 
@property (nonatomic, assign) UITextFieldViewMode clearButtonMode; 
@property (nonatomic, strong) NSString *text; 

- (id)initWithTextField:(UITextField *)textField; 
- (id)initWithFrame:(CGRect)frame; 

@end 

实现:

#import "TUTextField.h" 

@interface TUTextField() 

@property (nonatomic, strong) UITextField *textField; 

@end 

@implementation TUTextField 

- (id)initWithTextField:(UITextField *)textField 
{ 
    self = [super initWithFrame:textField.frame]; 
    if (self) { 
     self.textField = textField; 
     _textField.delegate = self; 

     _fixedDecimalPoint = NO; 
    } 
    return self; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.textField = [[UITextField alloc] initWithFrame:frame]; 
     _textField.delegate = self; 
     [self addSubview:_textField]; 

     _fixedDecimalPoint = NO; 
    } 
    return self; 
} 

- (void)setKeyboardType:(UIKeyboardType)keyboardType { 
    _textField.keyboardType = keyboardType; 
} 

- (void)setPlaceholder:(NSString *)placeholder { 
    _textField.placeholder = placeholder; 
} 

- (void)setBorderStyle:(UITextBorderStyle)borderStyle { 
    _textField.borderStyle = borderStyle; 
} 

-(void)setAdjustsFontSizeToFitWidth:(BOOL)adjustsFontSizeToFitWidth { 
    _textField.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth; 
} 

-(void)setClearButtonMode:(UITextFieldViewMode)clearButtonMode { 
    _textField.clearButtonMode = clearButtonMode; 
} 

-(void)setText:(NSString *)text { 
    _textField.text = text; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    [self addSubview:self.textField]; 
} 
*/ 

- (void)viewDidLoad { 
    [self addSubview:_textField]; 
} 

- (void)viewWillAppear { 
    [self addSubview:_textField]; 
} 


- (BOOL)textField:(UITextField *)textField 
shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string 
{ 
    if ([_delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) { 
     return [_delegate textField:textField shouldChangeCharactersInRange:range 
        replacementString:string]; 
    } 
    return YES; 
} 

- (BOOL)textFieldShouldClear:(UITextField *)textField { 
    if ([_delegate respondsToSelector:@selector(textFieldShouldClear:)]) { 
     return [_delegate textFieldShouldClear:textField]; 
    } 
    return YES; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    if ([_delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) { 
     [_delegate textFieldDidBeginEditing:textField]; 
    } 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    if ([_delegate respondsToSelector:@selector(textFieldDidEndEditing:)]) { 
     [_delegate textFieldDidEndEditing:textField]; 
    } 
} 

@end 

这里是代码,我的自定义包装添加到表视图:

} else if (indexPath.row == 1) { 
      cell.textLabel.text = @"Tax Rate"; 

      if (!_textField) { 
       UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(220.0, 8.0, 60.0, 26.0)]; 
       textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
       textField.adjustsFontSizeToFitWidth = YES; 
       textField.borderStyle = UITextBorderStyleRoundedRect; 
       textField.keyboardType = UIKeyboardTypeDecimalPad; 
       textField.placeholder = kTaxRateDefault; 

       _textField = [[TUTextField alloc] initWithTextField:textField]; 
       _textField.delegate = self; 

       // retrieve saved settings 
       _taxRate = [[SettingsSingleton sharedManager] taxRate]; 
       if (![_taxRate isEqualToString:@""]) 
        _textField.text = _taxRate; 
      } 

      UILabel *percentSign = [[UILabel alloc] initWithFrame:CGRectMake(285.0, 6.0, 20.0, 26.0)]; 
      percentSign.text = @"%"; 
      percentSign.backgroundColor = [UIColor clearColor]; 

      [cell addSubview:_textField]; 
      [cell addSubview:percentSign]; 
     } 
    } 

任何帮助表示赞赏!

回答

0

我不认为这是扩展UITextField的最佳方式。

而不是使一个NSObject与下它的UITextField的你应该说是这样的:

接口:

@interface UITextField (extendedUITextField) { 

    // Variables 

} 

//Methods 

@end 

实现:

@implementation UITextField (extendedUITextField) 

    // Method implement 

@end 

希望这有助于。

+0

其实我不能这样做,因为我需要能够拦截委托方法,并且UITextField不能成为它自己的委托:http://stackoverflow.com/q/1747777/347339 – Stunner

相关问题