Reputation: 14418
I have the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row == 0)
cell.tag = 0;
else {
cell.tag = 1;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *startDtLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)];
if (indexPath.row == 0)
startDtLbl.text = @"Username";
else {
startDtLbl.text = @"Password";
}
startDtLbl.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:startDtLbl];
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)];
passwordTF.delegate = self;
if (indexPath.row == 0)
passwordTF.tag = 2;
else {
passwordTF.tag = 3;
}
[cell.contentView addSubview:passwordTF];
}
return cell;
}
I'd like to get the UITextField, how can I do that? I've tried the following and it failed:
UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0];
UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1];
UITextField * username = (UITextField*)[username_cell.contentView viewWithTag:2];
UITextField * password = (UITextField*)[password_cell.contentView viewWithTag:3];
NSLog(@"Username is %@", [username text]);
NSLog(@"Password is %@", [password text]);
Upvotes: 4
Views: 10041
Reputation: 21
If your textfields are inside uiscrollview and incrementing dynamically
int i=0;
for (UIView *subview in ContentScroll.subviews) {
UITextField *textField = (UITextField*)[ subview viewWithTag:i];
NSLog(@"%@",textField.text);
NSString *fieldValue = textField.text;
if ([fieldValue isEqual:[NSNull null]]) {
NSLog(@"as");
}else{
NSLog(@"Field %d has value: %@", i, fieldValue);
}
i++;
}
Upvotes: 2
Reputation: 90117
You should stop to use tags to get cells. You should use indexPaths for this.
replace
UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0];
UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1];
with
NSIndexPath *indexPathUserName = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell * username_cell = [self.tableView cellForRowAtIndexPath:indexPathUserName];
NSIndexPath *indexPathPassword = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell * password_cell = [self.tableView cellForRowAtIndexPath:indexPathPassword];
and when you want to reference a specific view you can't use tag 0. Because all tags that don't have custom tags have a tag of 0. So if you use viewWithTag:0 you get the last added view. And usually that isn't the view you want.
Upvotes: 6
Reputation: 5432
I think the problem is on the on the first lin of this code:
Instead of this:
UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0];
UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1];
Use this:
UITableViewCell * username_cell = (UITableViewCell*)[self.tableView viewWithTag:0];
UITableViewCell * password_cell = (UITableViewCell*)[self.tableView viewWithTag:1];
Or whatever is the name of your tableview. Your are trying to get the UITableViewCells from self.view
, but UITableViewCells are the subviews of tableview.
Alternative
If this doesnot works, then you can find the UITablViewCells, using
NSArray *arrTableViewCells = [tableView subviews];
and then get the UITextFields from these cells.
It should work
Upvotes: -1
Reputation: 6161
The table appears to only present username and password so I would suggest keeping two pointers to the text fields that get created. This would eliminate the need for tags. The code for tagging the cells could be eliminated and the code that was tagging the text fields could be updated to store a pointer in the view's controller.
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)];
passwordTF.delegate = self;
if (indexPath.row == 0)
self.passwordTextField = passwordTF;
else {
self.usernameTextField = passwordTF;
}
[cell.contentView addSubview:passwordTF];
Upvotes: 1