2010-12-07 141 views
12

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

// 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,显示细腻并且点击时,如预期的键盘弹出,但我如何获得(得到的.text属性)中的UITextField每行包含?以及如何处理UITextFields的'textFieldShouldReturn'方法?

回答

21

我认为OP试图理解的是,一旦用户将数据输入到每个字段中,如何访问UITextField值。在@willcodejavaforfood所建议的单元格创建时这将不可用。

我一直在实现一个窗体并试图使其尽可能地方便用户。这是可行的,但请注意,根据您拥有的UITableViewCells/UITextField的数量,它会变得相当复杂。

首先你的问题重新:访问的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; 
     }   
     ... 
} 
+0

干杯Rog,我会尝试一下,当我回到我的苹果。我想知道Apple如何做到这一点,比如设置应用程序,我以为我会比这更简单/更清洁。我想他们可能会用完全不同的方式来做,而且都用代码。 – Darthtong 2010-12-07 12:18:41

8

在的cellForRowAtIndexPath:包括这个代码,

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

然后访问使用

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

如果您已经创建了一个类为您的自定义单元格我建议你反对它的工作文本框。

- (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; 
} 
2

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

1.创建一个自定义的细胞的UITableViewCell类,(egtextfieldcell )

2.Now,在textfieldcell.h文件调用textFieldDelegate

3.In的textfieldcell。米文件写入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.C reate自定义委托方法在MaintableViewController

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

7.In MaintableViewController.m文件写委托方法的实施,

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

8.call在细胞类委托方法,并传递在didendmethod

9.now变量,分配给自cell.delegate,在UITableView的初始化单元时

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

0

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

在故事板拖动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; 
} 
0

这是我设法让我在斯威夫特定制UITableViewCellUITextField内的文本。我在我的UIButton里面访问了另一个自定义UITableViewCell,在我的UITableViewController上有@IBAction。我在我的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内更新值。