iPhone:如何在自定义单元格的动态数量上管理 UITextfield 委托(delegate)方法

标签 iphone ios ios4

我的表格 View 中有动态数量的文本字段,我将每个文本字段放入 IB 中的自定义单元格中,并通过 nibName 加载单元格。

我想在用户输入数据时验证并显示警报,同样在 editingisDone 时我想从用户那里获取输入值并将其保存到相关对象中。

例如,这些是我可以使用的一些委托(delegate)方法:

- (void)textFieldDidEndEditing:(UITextField *)textField{
   //save the data
}

- (IBAction)textFieldDoneEditing:(id)sender {
    //hide the keypad when done is pressed
    [sender resignFirstResponder];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange{}

2个问题:

1-当获取用户输入并验证输入时,我如何知道触发了哪个文本字段的委托(delegate),因为单元格和文本字段的数量是动态的,我该如何管理它?

2-我这样做是为了隐藏键盘,但不确定这样做是否正确; - 在 IB 中,我打开了 customcell--> 右键单击​​ uitextfield 并将其 didEndonExit 连接到 FirstResponder 的 textFieldDoneEditing 方法。这有效,但如果我没有向文本字段添加任何字符,我将无法返回。所以它强制写一些东西才能按下按钮。

最佳答案

关于你的第一个问题......

在下面的代码中,我假设每个单元格中都有一个 UITextField。我还假设您已经创建了一个名为 CustomCellUITableViewCell 子类,其中包含一个 UITextField

#pragma mark - UITableViewDataSource 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"CellIdentifier";
   CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (!cell)
   {
      cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:CellIdentifier] autorelease];
      cell.textField.tag = indexPath.row;
      cell.textField.delegate = self;
   }
   return cell; 
}

#pragma mark - UITextFieldDelegate 

- (void)textFieldDidEndEditing:(UITextField *)textField
{
   NSLog(@"textField tag: %d", textField.tag); // this will show which textField did end editing ...
}

关于iPhone:如何在自定义单元格的动态数量上管理 UITextfield 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7050215/

相关文章:

ios - iOS 9.1 启动屏幕上的黑色方 block

ios - 使用未指定的索引。考虑添加 ".indexOn": "phone" at/use_frameworks_beta_2/searchIndex to your security rules for better performance

objective-c - 如何调试引用项目中的崩溃?

iphone - UIPickerView selectRow时触发代码 :inComponent:animated:YES is completed?

iphone - 为 MDM(移动设备管理)设置 Snow Leopard 服务器

javascript - 在 iOS 应用程序中嵌入 JavaScript 引擎

ios - 并发请求中的进度条

ios - 如何生成包含详细信息的二维码

ios - 如何从 ViewController 切换回 TabBarController 的第二个选项卡

ios - iOS 应用程序内的新 field ?