0

当 UITableView 进入编辑模式时,我想使用自动布局将 UITextField 放置在 cell.textLabel 旁边。我的代码工作正常,但我在日志中收到一条消息,指出必须打破一些现有的约束。

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 视频(也许我只是个白痴)。谢谢你的帮助。

4

1 回答 1

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];
    }
}
于 2012-10-25T12:50:34.403 回答