2013-04-15 59 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

相關問題