Reputation: 1580
I have one UITableView and in that UITableViewCell am dynamically creating "n" number of UITextFields. How do i get the values i entered into UITextField by using the tag?.
This is how i create UITextField dynamcally
-(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;
}
This is how i tried to access UITextField Value but its crashing.
NSString *text = [(UITextField *)[cell.contentView viewWithTag:index] text];
Were index
(tag) is getting from a for loop .
-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360'
Please help me to fix this
displayTextfield() calling from here
- (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;
}
Upvotes: 1
Views: 414
Reputation: 52203
Zeroth index in cell.contentView.subviews
is already taken by a special view named UITableViewCellContentView
so the following line doesn't return a UITextField when indexPath.row
is 0
:
>>> NSLog("%@", [cell.contentView viewWithTag:0])
<UITableViewCellContentView: 0x7fb7c2eb1cd0; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x7fb7c2eb22a0>; layer = <CALayer: 0x7fb7c2eb1e40>
Change your tag value to something greater and this should fix the issue:
[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];
so when you need to get the UITextField again:
NSString *text = [(UITextField *)[cell.contentView viewWithTag:indexPath.row + 1] text];
Upvotes: 1
Reputation: 3319
Fix your function's first line to be:
UITextField * textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
Secondly, assign tag +1 to the textfield so it will be non-zero:
[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];
Lastly, before accessing the textfield text property, make sure it is actually a textfield and that the view exist:
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...
}
Upvotes: 1