如何使用标签动态创建 UITextField (在 UITableViewCell 之上)文本值?
How to get dynamically created UITextField (on top of UITableViewCell ) text value using tag?
我有一个 UITableView,在该 UITableViewCell 中动态创建 "n" 个 UITextField。如何使用标签获取输入到 UITextField 中的值?
这就是我动态创建 UITextField 的方式
-(UITextField *)displayTextfield:(NSUInteger)index{
textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
textField.placeholder=@"Text here";
textField.tag=index;
textField.delegate=self;
textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
textField.textAlignment=UITextAlignmentLeft;
textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
textField.textColor=[UIColor darkGrayColor];
textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
return textField;
}
这就是我尝试访问 UITextField 值但崩溃的方式。
NSString *text = [(UITextField *)[cell.contentView viewWithTag:index] text];
index
(标签)来自 for 循环。
-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360'
请帮我解决这个问题
displayTextfield() 从这里调用
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier;
if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
MyIdentifier = @"CharCell";
}else{
MyIdentifier = @"IntCell";
}
cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
[cell.contentView addSubview:[self displayTextfield:indexPath.row]];
}
return cell;
}
cell.contentView.subviews
中的第零索引已被名为 UITableViewCellContentView
的特殊视图占用,因此当 indexPath.row
为 [=16] 时,以下行不会 return UITextField =]:
>>> NSLog("%@", [cell.contentView viewWithTag:0])
<UITableViewCellContentView: 0x7fb7c2eb1cd0; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x7fb7c2eb22a0>; layer = <CALayer: 0x7fb7c2eb1e40>
将您的标签值更改为更大的值,这应该可以解决问题:
[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];
因此当您需要再次获取 UITextField 时:
NSString *text = [(UITextField *)[cell.contentView viewWithTag:indexPath.row + 1] text];
将函数的第一行修改为:
UITextField * textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
其次,将标签 +1 分配给文本字段,使其不为零:
[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];
最后,在访问文本字段文本 属性 之前,确保它实际上是一个文本字段并且视图存在:
NSString * textFieldText = nil;
UIView * view = [cell.contentView viewWithTag:index];
if(view && [view isKindOfClass:[UITextField class]]) {
UITextField * textField = (UITextField *)view;
textFieldText = [textField text];
}
if(textFieldText) {
// Use text here...
}
我有一个 UITableView,在该 UITableViewCell 中动态创建 "n" 个 UITextField。如何使用标签获取输入到 UITextField 中的值?
这就是我动态创建 UITextField 的方式
-(UITextField *)displayTextfield:(NSUInteger)index{
textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
textField.placeholder=@"Text here";
textField.tag=index;
textField.delegate=self;
textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
textField.textAlignment=UITextAlignmentLeft;
textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
textField.textColor=[UIColor darkGrayColor];
textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
return textField;
}
这就是我尝试访问 UITextField 值但崩溃的方式。
NSString *text = [(UITextField *)[cell.contentView viewWithTag:index] text];
index
(标签)来自 for 循环。
-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360'
请帮我解决这个问题
displayTextfield() 从这里调用
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier;
if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
MyIdentifier = @"CharCell";
}else{
MyIdentifier = @"IntCell";
}
cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
[cell.contentView addSubview:[self displayTextfield:indexPath.row]];
}
return cell;
}
cell.contentView.subviews
中的第零索引已被名为 UITableViewCellContentView
的特殊视图占用,因此当 indexPath.row
为 [=16] 时,以下行不会 return UITextField =]:
>>> NSLog("%@", [cell.contentView viewWithTag:0])
<UITableViewCellContentView: 0x7fb7c2eb1cd0; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x7fb7c2eb22a0>; layer = <CALayer: 0x7fb7c2eb1e40>
将您的标签值更改为更大的值,这应该可以解决问题:
[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];
因此当您需要再次获取 UITextField 时:
NSString *text = [(UITextField *)[cell.contentView viewWithTag:indexPath.row + 1] text];
将函数的第一行修改为:
UITextField * textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
其次,将标签 +1 分配给文本字段,使其不为零:
[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];
最后,在访问文本字段文本 属性 之前,确保它实际上是一个文本字段并且视图存在:
NSString * textFieldText = nil;
UIView * view = [cell.contentView viewWithTag:index];
if(view && [view isKindOfClass:[UITextField class]]) {
UITextField * textField = (UITextField *)view;
textFieldText = [textField text];
}
if(textFieldText) {
// Use text here...
}