【发布时间】:2015-12-06 06:54:47
【问题描述】:
我有一个 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;
}
【问题讨论】:
-
你在哪里调用
displayTextfield函数?你也可以给我们看看那部分吗? -
@ozgur 更新问题
标签: ios objective-c uitableview uitextfield