UITableViewCell帮助中的UITextField

我已经search了互联网寻找一个很好的教程或发布有关每个单元格中填入UITextField的UITableView用于数据input。

我想跟踪每个UITextField和滚动时写入的文本。 tableView将被分割。 我一直在使用自定义的UITableViewCell,但我打开任何方法。

另外,是否有可能使用textFields作为ivars?

如果你们中的任何一个人都能指出我的方向,那将不胜感激。

先谢谢你!

为了解决您的问题,您必须维护一个数组,其中包含一些数字(您添加到所有单元格中的textField的数量)。

创build该数组时,需要将空的NSString对象添加到该数组中。 并且每次加载单元格时都必须将尊重的对象replace为受尊敬的textField。

检查下面的代码。

- (void)viewDidLoad{ textFieldValuesArray = [[NSMutableArray alloc]init]; for(int i=0; i<numberofRows*numberofSections; i++){ [textFieldValuesArray addObject:@""]; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return numberofSections; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numberofRows; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; CustomTextField *tf = [[CustomTextField alloc] initWithFrame:CGRectMake(5,5,290,34)]; tf.tag = 1; [cell.contentView addSubView:tf]; [tf release]; } CustomTextField *tf = (CustomTextField*)[cell viewWithTag:1]; tf.index = numberofSections*indexPath.section+indexPath.row; tf.text = [textFieldValuesArray objectAtIndex:tf.index]; return cell; } - (void)textFieldDidEndEditing:(UITextField *)textField{ int index = textField.index; [textFieldValuesArray replaceObjectAtIndex:index withObject:textField.text]; } 

问候,

萨蒂亚

首先,你必须明白,UITableViewCell和UITextField只是视图,他们不应该保存数据,他们只是应该显示它们,并允许用户与他们交互:数据应该保持存储在表中的控制器视图。

你必须记住,UITableView允许你重用UITableViewCell实例的性能目的:屏幕上显示的实际上是唯一的子视图UITableView保持在那里。 这意味着您将重新使用一个已经具有文本字段的单元格,并直接在该字段上设置文本。 当用户点击该字段时,将编辑它,当用户完成时,您将不得不从该字段中取回数值。

最快的方法是使用Satya提出的build议,即构build普通的UITableViewCell并插入到UITextField中(不需要CustomTextField类…)。 该标签将允许您轻松地返回文本字段…但是,您必须设置文本字段,以便在表格视图resize或同一单元格中的标签更改时正常运行。

最简单的方法是为UITableViewCell创build子类并设置标签和文本字段的布局,并且可以将该文本字段作为自定义子类的属性。

我已经在tableview中使用Textfields进行数据input。 我已经在名为Utility的单独的类中定制了UITextField类:

在Utility.h中

  @interface CustomUITextField:UITextField{ NSInteger rowNumber; } 

在Utility.m中

 @implementation CustomUITextField @synthesize rowNumber; @end 

我的tableView cellForRowAtIndexPath方法是

 - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Identifier = @"Cell"; UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:Identifier]; if(cell == nil) cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath]; CustomUITextField *itemNameTextField = (CustomUITextField *)[cell.contentView viewWithTag:TEXTFIELD_TAG];//this is the tag I have set in reuseTableViewCellWithIdentifier method for textfield itemNameTextField.rowNumber = indexPath.row; itemNameTextField.text = @"";//you can set it for the value you want if(itemListTable.editing) itemNameTextField.borderStyle = UITextBorderStyleRoundedRect; else itemNameTextField.borderStyle = UITextBorderStyleNone; return cell; } 

您可以为CustomUITextField自定义UITextField的委托方法,并可以通过访问CustomTextField的行号来保存在特定行的文本字段中input的文本。

试试这个。

我有同样的问题,这里是我发现的一些代码,对待这个问题。 它将数据input到数组中查看debugging器控制台,以查看要input的文本的结果,这里是TextFieldCell链接。 。 快乐编码