2012-10-24 84 views
0

我想在UITableView進入編輯模式時使用自動佈局將UITextField放置在cell.textLabel旁邊。我有正常工作的代碼,但我在日誌中收到一條消息,指出一些現有約束條件必須被破壞。使用Autolayout將UITextField放在UILabel旁邊

UILabel *label = cell.textLabel; 
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 22.0)]; 
textField.placeholder = cell.textLabel.text; 
textField.translatesAutoresizingMaskIntoConstraints = NO; 
textField.text = text; 
[cell.contentView addSubview:textField]; 
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label]-[textField]-|" 
                  options:0 
                  metrics:nil 
                   views:NSDictionaryOfVariableBindings(label,textField)]]; 
[cell addConstraint:[NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]]; 

日誌信息是:

(
"<NSAutoresizingMaskLayoutConstraint:0x1066abc0 h=--& v=--& H:[UITableViewCellContentView:0x9a7d070(934)]>", 
"<NSAutoresizingMaskLayoutConstraint:0x10660a90 h=--& v=--& H:[UILabel:0x9a93ef0(914)]>", 
"<NSLayoutConstraint:0x1065ea60 H:[UITextField:0x1065c660]-(NSSpace(20))-| (Names: '|':UITableViewCellContentView:0x9a7d070)>", 
"<NSAutoresizingMaskLayoutConstraint:0x10660a50 h=--& v=--& UILabel:0x9a93ef0.midX == + 467>", 
"<NSLayoutConstraint:0x10669280 H:[UILabel:0x9a93ef0]-(NSSpace(8))-[UITextField:0x1065c660]>" 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10669280 H:[UILabel:0x9a93ef0]-(NSSpace(8))-[UITextField:0x1065c660]> 

我認爲這是由與cell.textLabel寬度衝突的限制造成的。所以我的問題是,如果有另一種方法可以在標準表視圖單元格中具有文本字段,該單元格可以從textLabel寬度延伸到單元格末尾而不會違反默認約束。我覺得我很接近,但我不能完全達到目標。我已經搜索了三個星期的谷歌,但不能讓我的頭在這附近。我還觀看了自動佈局的WWDC視頻(也許我只是一個白癡)。謝謝你的幫助。

回答

1

與其試圖操縱Apple的標準單元格,我冒險嘗試編寫了自己的UITableViewCell子類,它模仿了UITableViewCellStyleValue1。當tableview進入編輯模式時,用最簡單的術語來隱藏值標籤並顯示文本字段。對於那些誰可能有同樣的事情掙扎,我張貼一些代碼,以幫助您開始:

@interface NXAlphaNumericTextFieldCell : UITableViewCell<UITextFieldDelegate,NumberKeyboardDelegate> 

@property (strong, nonatomic) UITextField *inputTextField; 
@property (strong, nonatomic) UILabel *titleLabel; 
@property (strong, nonatomic) UILabel *valueLabel; 

@property (strong, nonatomic) NSArray *xTitleLabelConstraints; 
@property (strong, nonatomic) NSArray *xTextFieldConstraints; 
@property (strong, nonatomic) NSArray *xValueLabelConstraints; 

@end 

並在實施的幾個方法:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)]; 
     self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO; 
     self.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f]; 
     self.titleLabel.backgroundColor = [UIColor clearColor]; 

     self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)]; 
     self.valueLabel.translatesAutoresizingMaskIntoConstraints = NO; 
     self.valueLabel.textColor = [UIColor colorWithRed:81.0/255.0 green:102.0/255.0 blue:145.0/255.0 alpha:1.0]; 
     self.valueLabel.backgroundColor = [UIColor clearColor]; 

     self.inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 44.0f)]; 
     self.inputTextField.translatesAutoresizingMaskIntoConstraints = NO; 
     self.inputTextField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
     self.inputTextField.autocorrectionType = UITextAutocorrectionTypeYes; 
     self.inputTextField.clearButtonMode = UITextFieldViewModeAlways; 
     self.inputTextField.delegate = self; 

     [self.contentView addSubview:self.valueLabel]; 
     [self.contentView addSubview:self.titleLabel]; 
     [self.contentView addSubview:self.inputTextField]; 

     UILabel *textLabel = self.titleLabel; 
     NSDictionary *labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(textLabel); 
     self.xTitleLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[textLabel]" 
                      options:0 
                      metrics:nil 
                       views:labelTextFieldViewsDictionary]; 
     UITextField *textfield = self.inputTextField; 
     labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(textLabel, textfield); 
     self.xTextFieldConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[textLabel]-50-[textfield]-|" 
                       options:0 
                       metrics:nil 
                        views:labelTextFieldViewsDictionary]; 
     UILabel *valueLabel = self.valueLabel; 
     labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(valueLabel); 
     self.xValueLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[valueLabel]-|" 
                     options:0 
                     metrics:nil 
                      views:labelTextFieldViewsDictionary]; 

     [self setNeedsUpdateConstraints]; 
    } 
    return self; 
} 


- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
    if (self.isEditing) { 
     [self.inputTextField becomeFirstResponder]; 
    } 
} 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 

    [self addConstraints:self.xTitleLabelConstraints]; 
    if (editing) { 
     if (self.inputType == kCellInputTypeAlphaNumeric) { 
      self.inputTextField.keyboardType = UIKeyboardTypeAlphabet; 
     } else if (self.inputType == kCellInputTypeEmail) { 
      self.inputTextField.keyboardType = UIKeyboardTypeEmailAddress; 
     } else if (self.inputType == kCellInputTypePhoneNumber) { 
      self.inputTextField.keyboardType = UIKeyboardTypeNamePhonePad; 
     } else { 
      if (!self.numberKeyboard) { 
       self.numberKeyboard = [[NumberKeyboard alloc] initWithNibName:@"NumberKeyboard" bundle:nil]; 
       self.numberKeyboard.textField = self.inputTextField; 
       self.numberKeyboard.showsPeriod = YES; 
       self.numberKeyboard.delegate = self; 
      } 
      self.inputTextField.inputView = self.numberKeyboard.view; 
     } 
     self.inputTextField.text = self.valueLabel.text; 
     self.inputTextField.placeholder = self.titleLabel.text; 
     self.valueLabel.hidden = YES; 
     self.inputTextField.hidden = NO; 

     [self removeConstraints:self.xValueLabelConstraints]; 
     [self addConstraints:self.xTextFieldConstraints]; 
    } else { 
     [self.inputTextField resignFirstResponder]; 
     self.inputTextField.hidden = YES; 
     self.valueLabel.hidden = NO; 
     [self removeConstraints:self.xTextFieldConstraints]; 
     [self addConstraints:self.xValueLabelConstraints]; 
    } 
} 

- (void)updateConstraints 
{ 
    [super updateConstraints]; 

    if (self.editing) { 
     [self removeConstraints:self.xValueLabelConstraints]; 
     [self addConstraints:self.xTextFieldConstraints]; 
    } else { 
     [self removeConstraints:self.xTextFieldConstraints]; 
     [self addConstraints:self.xValueLabelConstraints]; 
    } 
}