在自定义UITableViewCell中访问UITextField

Dar*_*ong 12 iphone objective-c uitableview ios

我在IB中创建了一个包含UITextField的UITableViewCell(带有关联的UITableViewCell子类,.m和.h).此UITextField连接到UITableViewCell子类中的IBOutlet,并且还具有属性.在我的表视图控制器中,我使用此自定义单元格,其代码如下:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textfieldTableCell"];
    if (cell == nil) {
        // Create a temporary UIViewController to instantiate the custom cell.
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"TextfieldTableCell" bundle:nil];
        // Grab a pointer to the custom cell.
        cell = (TextfieldTableCell *)temporaryController.view;  
        // Release the temporary UIViewController.
        [temporaryController release];
    }

    return cell;

}
Run Code Online (Sandbox Code Playgroud)

UITextField显示正常,键盘按预期弹出,但如何访问(获取.text属性)每行包含的UITextField?以及如何处理UITextFields的'textFieldShouldReturn'方法?

Rog*_*Rog 22

我认为OP试图理解的是,一旦用户在每个字段中输入数据,如何访问UITextField值.根据@willcodejavaforfood的建议,在创建单元格时将无法使用此功能.

我一直在实现一个表单,并尽可能使用户友好.这是可行的,但请注意,根据您拥有的UITableViewCells/UITextField的数量,它可能会变得非常复杂.

首先回答你的问题:访问UITextField的值:

1)使你的视图控制器a <UITextFieldDelegate>

2)实现以下方法:

- (void) textFieldDidEndEditing:(UITextField *)textField {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; // this should return you your current indexPath

        // From here on you can (switch) your indexPath.section or indexPath.row
        // as appropriate to get the textValue and assign it to a variable, for instance:
    if (indexPath.section == kMandatorySection) {
        if (indexPath.row == kEmailField) self.emailFieldValue = textField.text;
        if (indexPath.row == kPasswordField) self.passwordFieldValue = textField.text;
        if (indexPath.row == kPasswordConfirmField) self.passwordConfirmFieldValue = textField.text;
    }
    else if (indexPath.section == kOptionalSection) {
        if (indexPath.row == kFirstNameField) self.firstNameFieldValue = textField.text;
        if (indexPath.row == kLastNameField) self.lastNameFieldValue = textField.text;
        if (indexPath.row == kPostcodeField) self.postcodeFieldValue = textField.text;
    }   
}
Run Code Online (Sandbox Code Playgroud)

我还使用类似的语法来确保当前编辑的字段可见:

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    CustomCell *cell = (CustomCell*) [[textField superview] superview];
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

最后,你可以textViewShouldReturn:用类似的方式处理:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]];
    switch (indexPath.section) {
        case kMandatorySection:
        {
            // I am testing to see if this is NOT the last field of my first section
            // If not, find the next UITextField and make it firstResponder if the user
            // presses ENTER on the keyboard
            if (indexPath.row < kPasswordConfirmField) {
                NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
                CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:sibling];
                [cell.cellTextField becomeFirstResponder];
            } else {
                // In case this is my last section row, when the user presses ENTER, 
                // I move the focus to the first row in next section
                NSIndexPath *sibling = [NSIndexPath indexPathForRow:kFirstNameField inSection:kOptionalSection];
                MemberLoginCell *cell = (MemberLoginCell*)[self.memberTableView cellForRowAtIndexPath:sibling];
                [cell.cellTextField becomeFirstResponder];
            }
            break;
        }           
        ...
}
Run Code Online (Sandbox Code Playgroud)


Kin*_*iss 8

在cellForRowAtIndexPath中:包含此代码,

yourTextField.tag=indexPath.row+1; //(tag must be a non zero number)
Run Code Online (Sandbox Code Playgroud)

然后使用访问textField

UITextField *tf=(UITextField *)[yourView viewWithTag:tag];
Run Code Online (Sandbox Code Playgroud)