Weston
Weston

Reputation: 1491

How can I edit text in a UITextField when its inside a UITableViewCell?

I am trying to create a form like tableview, I have seen this a thousand times in other apps, so I figured it would be simple.

My attempt is below. I added a UITextField to the content view of a UITableViewCell. I have also tried implementing UITextFieldDelegate methods and setting selection style to none.

can anyone see what I am doing wrong?

I am using the canned UITableViewController with a separate NSObject to handle the delegate and datasource methods, and a custom UITableViewCell subclass.

Right now when I tap the cells, they do not becomeFirstResponder, nothing happens at all. I tried setting some breakpoints in setSelected, but it does not appear to be triggered.

My tableViewCell code

@implementation HSTableViewCell

- (void)willMoveToSuperview:(UIView *)newSuperview {

    [self.contentView addSubview:self.textField];

    NSDictionary *elements = NSDictionaryOfVariableBindings(_textField);

    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_textField]|"
                                                                           options:NSLayoutFormatDirectionLeftToRight
                                                                           metrics:nil
                                                                             views:elements];

    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_textField]-|"
                                                                             options:NSLayoutFormatDirectionLeftToRight
                                                                             metrics:nil
                                                                               views:elements];

    [self.contentView addConstraints:verticalConstraints];
    [self.contentView addConstraints:horizontalConstraints];

}

- (void)prepareForReuse {
    [self.textField removeFromSuperview];
    [self.contentView removeConstraints:self.contentView.constraints];
    self.textField = nil;
}


- (UITextField *)textField {
    if (!_textField) {
        _textField = [UITextField new];
        [_textField setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_textField setPlaceholder:@"Test"];
    }
    return _textField;
}


@end

UITableViewController init

- (UITableViewController *)tableViewController {
    if (!_tableViewController) {
        _tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        [_tableViewController.tableView setDataSource:self.tableViewDataManager];
        [_tableViewController.tableView setDelegate:self.tableViewDataManager];
        [_tableViewController.tableView registerClass:[HSTableViewCell class] forCellReuseIdentifier:identifer];
    }
    return _tableViewController;
}

and UITableViewDelegate/Datasource

@implementation HSTableViewDataManger


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath];
    return cell;
}

@end

If anyone is interested, the test project is below

This is VERY barebones, I am trying to isolate exactly what I need to achieve the desired behavior.

https://dl.dropboxusercontent.com/u/2396540/TextFieldInCellTest.zip

Upvotes: 0

Views: 257

Answers (1)

rdelmar
rdelmar

Reputation: 104092

I'm not sure why adding the text views in willMoveToSuperview doesn't work, but that's an unusual place to do that. You should put the code you have there into initWithStyle:reuseIdentifier:, and you should delete the prepareForReuse method (the text fields will only be added once in the init method, so there's no need to delete them). That should give you text fields that are editable.

Upvotes: 1

Related Questions