Iphone UITableViewCell帮助中的UITextField
我在互联网上搜索了一篇关于在每个单元格中填充UITextField以进行数据输入的UITableView的好教程或帖子 我希望在滚动时跟踪每个UITextField和其中写入的文本。tableView将被剖切。我一直在使用自定义UITableViewCell,但我对任何方法都持开放态度 此外,是否可以将文本字段用作IVAR 如果你们中有人能给我指出正确的方向,我将不胜感激Iphone UITableViewCell帮助中的UITextField,iphone,ipad,uitableview,ios,uitextfield,Iphone,Ipad,Uitableview,Ios,Uitextfield,我在互联网上搜索了一篇关于在每个单元格中填充UITextField以进行数据输入的UITableView的好教程或帖子 我希望在滚动时跟踪每个UITextField和其中写入的文本。tableView将被剖切。我一直在使用自定义UITableViewCell,但我对任何方法都持开放态度 此外,是否可以将文本字段用作IVAR 如果你们中有人能给我指出正确的方向,我将不胜感激 提前谢谢你 要解决您的问题,您必须维护一个数组,其中包含一定数量(添加到所有单元格中的文本字段数量)的对象 创建该数组时,需
提前谢谢你 要解决您的问题,您必须维护一个数组,其中包含一定数量(添加到所有单元格中的文本字段数量)的对象 创建该数组时,需要将空NSString对象添加到该数组中。每次加载单元格时,都必须将受尊重的对象替换为受尊重的文本字段 检查以下代码
- (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];
}
-(void)viewDidLoad{
textFieldValuesArray=[[NSMutableArray alloc]init];
对于(int i=0;i首先,您必须了解UITableViewCell和UITextField只是视图,它们不应该保存数据,只应该显示它们并允许用户与它们交互:数据应该存储在表视图的控制器中
您必须记住,UITableView允许您出于性能目的重用UITableViewCell实例:屏幕上显示的内容实际上是UITableView保留的唯一子视图。这意味着您将重用一个已经包含文本字段的单元格,并直接在该字段上设置文本。当用户点击该字段时,它将被删除将对其进行编辑,用户完成后,您必须从中获取值
最快的方法是使用Satya提出的方法,即构建普通的UITableViewCell并插入UITextField(不需要CustomTextField类…)。该标记将允许您轻松返回文本字段…但您必须设置文本字段,以便在表视图调整大小或同一单元格中的标签更改时,该字段能够正常工作
最干净的方法是将UITableViewCell子类化并设置标签和文本字段的布局,您可以将文本字段作为自定义子类的属性提供。我在tableview中使用了文本字段进行数据输入。
我已在一个名为“实用程序”的单独类中自定义UITextField类:
在实用程序中
@interface CustomUITextField:UITextField{
NSInteger rowNumber;
}
实用的
@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的行号,可以保存在特定行的textfield中输入的文本
试试这个。我也遇到了同样的问题。我发现了一些处理这个问题的代码。它将输入的数据放在一个数组中。查看调试器控制台,查看键入文本的结果,这是链接。快乐编码如果我在cellForRow中使用常规的switch语句,这对分区表视图有效吗我也会为分区tableview工作,但我们需要维护某些索引。我会更改代码检查它。“cell=[[UITableView alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];”?我在这个线程中发布了这个问题的延续。最好使用@property(非原子,保留)NSIndexPath*indexPath;