2012-10-24 51 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]; 
    } 
}