在自定义的UITableViewCell中访问UITextField

我有一个UITableViewCell(与相关联的UITableViewCell子类,.m和.h)在IB中创build,其中包含一个UITextField。 这个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; } 

UITextField显示正常,当按下button时popup键盘,但是如何访问(get .text属性)每行包含的UITextField? 以及如何处理UITextFields的“textFieldShouldReturn”方法?

我认为OP想要了解的是用户在每个字段中input数据后如何访问UITextField值。 在@willcodejavaforfood所build议的单元格创build时,这将不可用。

我一直在实施一个表格,并尽可能使用户友好。 这是可行的,但要注意,它可以变得相当复杂,取决于你有多lessUITableViewCells / UITextFields。

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

1)使你的视图控制器是一个<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; } } 

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

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

最后,您可以处理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; } ... } 

在cellForRowAtIndexPath中:包含这个代码,

 yourTextField.tag=indexPath.row+1; //(tag must be a non zero number) 

然后使用访问textField

 UITextField *tf=(UITextField *)[yourView viewWithTag:tag]; 

解决这两个问题有更简单的方法,

1.为单元格创build一个自定义的uitableviewCell类,(例如textfieldcell)

2.现在,在textfieldcell.h文件中调用textFieldDelegate

3.在textfieldcell.m文件中写入textFieldDelegate方法即

 -(BOOL)textFieldShouldReturn:(UITextField *)textField; -(void)textFieldDidEndEditing:(UITextField *)textField; 
  1. (第一个问题)现在,在
  -(BOOL)textFieldShouldReturn:(UITextField *)textField { [self.mytextBox resignFirstResponder]; return YES; } 

5.(第二个问题),

 -(void)textFieldDidEndEditing:(UITextField *)textField { nameTextField = mytextBox.text; } 

6.在MaintableViewController中创build一个自定义的委托方法

 @protocol textFieldDelegate <NSObject> -(void)textName:(NSString *)name; @end 

7.在MaintableViewController.m文件中写入委托方法的实现,

 -(void)textName:(NSString *)name{ Nametext = name; NSLog(@"name = %@",name); } 

8.在cell类中调用委托方法,并在dndnd方法中传递variables

9.现在,在uitableview中初始化单元格时,将自身赋值给cell.delegate

10.thats它得到了从文本字段传递到主视图的variables,现在做任何你想要的variables

如果你已经为你的自定义单元格创build了一个类,我build议你去处理它。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCustomCell* cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"]; if (cell == nil) { // Load the top-level objects from the custom cell XIB. NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). cell = (MyCustomCell *) [topLevelObjects objectAtIndex:0]; } // This is where you can access the properties of your custom class cell.myCustomLabel.text = @"customText"; return cell; } 

本教程对我有帮助。 你可以通过标签来引用你需要的任何对象。

在故事板拖动UIImageViewUITextField等,并设置标记为100(无论你想要的),然后在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用标记来引用它。

下面是你可以做的事情,只要记住在故事板中设置标签:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } UITextField *tField = (UITextField *)[cell viewWithTag:100]; return cell; } 

这是我设法得到我的自定义UITableViewCell在Swift中的UITextField内的文本。 我在我的UIButton里面访问了另一个在我的UITableViewController上有一个@IBAction自定义的UITableViewController 。 我在我的UITableViewController只有一个部分,但无论如何这并不重要,因为您可以轻松地自行设置和分配它。

 @IBAction func submitButtonTapped(sender: UIButton) { print("Submit button tapped") let usernameCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! UsernameTableViewCell print("Username: \(usernameCell.usernameTextField.text)") } 

每当我点击我的UIButton ,它给了我的UITextField里面的文本的更新值。