views:

690

answers:

3

I have UITableView that has custom cells representing a player. When I add a new player a new data item is added and [UITableView reloadData] is called. Part of the cell is a UITextView that contains the player name. How do I bring up the keyboard for it?

I could use becomeFirstResponder if I had access to the UITextView in the cell but it hasn't actually been created yet. Since reloadData doesn't actually perform the creation of the new cell yet.

Someone else must have solved this before. Any tips on how to make this work would be greatly appreciated.

My code snippet:

-(IBAction)addPlayerPressed:(id)sender {
Player *newPlayer = [Player alloc];
[players addObject:newPlayer];
[table reloadData];

// Scroll to bottom showing the new player location
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([players count] - 1) inSection:0];
[table scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

// cell is always nil  :(
PlayerTableCell *cell = (PlayerTableCell *)[table cellForRowAtIndexPath:scrollIndexPath]
[cell.nameField setfirstResponder];
}
A: 

Can u make the textfields delegate the UITableviewController instead Of the table cell?

Daniel
A: 

In your PlayerTableCell class, you could put some code in the -initWithStyle:reuseIdentifier: method that checks to see if it should become first responder and, if so, does. You probably have a PlayerController class or something similar; you might make a delegate method for the custom table view cell or something similar.

Jeff Kelley
+2  A: 

Couple of things:

You're probably better off using UITextField instead of UITextView. UITextField is ideal for single-line entries like what you're describing here.

As for your solution, here's what I'd recommend. (Also took the liberty of cleaning up your memory management)

Add a BOOL property to your Player class called, say, 'needsKeyboardDisplay', then set it to yes after you create a new instance.

-(IBAction)addPlayerPressed:(id)sender {
Player *newPlayer = [[Player alloc] init];


newPlayer.needsKeyboardDisplay = YES;


[players addObject:newPlayer];

[newPlayer release];

[table reloadData];

// Scroll to bottom showing the new player location

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([players count] - 1) inSection:0];

[table scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];


}

Then, in cellForRowAtIndexPath, insert this after you've otherwise set up your cell:

Player *playerForCell = [players objectAtIndex:indexPath.row]; 

if (playerForCell.needsKeyboardDisplay)
{
[nameField becomeFirstResponder];
playerForCell.needsKeyboardDisplay = NO;
}

All that said, from a user experience perspective, the (somewhat) standard iPhone experience for editing details in a long list of items like this is to do it in another view. It's up to you, of course.

Danilo Campos
Works perfectly now! Thanks for the help!!!
I take that back, this fix seems to seriously break the UITableView. After adding and removing a few rows the display gets very confused. My data sets and the the actual cells reported as rendered appear correct but the display itself shows duplicate entries. Commenting out the code that shows the keyboard restores the app to normal functionality. :(