问题
I have an app built for iOS 5 that I'm trying to upgrade straight to iOS 7, so this also maybe an issue with iOS 6.
We have a UITextField inside a custom table view cell (class derived from UITableCellView), but tapping on it no longer brings up the keyboard in the simulator. Everything is enabled, and User Interaction Enabled is checked.
It used to work fine in iOS 5.
I'm not sure what code to include, but here's the code that creates the cell... the LoginRegisterTableViewCell just has a 'fieldLabel' (UILabel) and 'userText' (UITextField):
// Login area
static NSString * reuseIdentifier = @"LoginRegisterTableViewCell";
LoginRegisterTableViewCell * cell = (LoginRegisterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(cell == nil)
{
// The official Apple way of loading TableViewCell nibs
// http://www.nomadplanet.fr/2011/01/custom-uitableviewcells-loaded-from-xib-howto-debug/
[[NSBundle mainBundle] loadNibNamed:@"LoginRegisterTableViewCell" owner:self options:nil];
cell = formFieldCell;
self.formFieldCell = nil;
}
cell.delegate = self;
cell.userText.tag = [indexPath row];
I can get the keyboard to come up if I call [userText becomeFirstResponder] when the table cell is selected, but this seems like a workaround as opposed to the correct way.
回答1:
Try this code with the table view data source: cellForRowAtIndexPath
NSString *cellReuseIdentifier = @"CellIdentifier";
UINib *nib = [UINib nibWithNibName:@"CustomTableViewCell" bundle:nil];
[_myTableView registerNib:nib forCellReuseIdentifier:cellReuseIdentifier];
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (!cell)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
return cell;
It is working for me with the custom cell created with the xib as shown in the image

Note: Mark Also create XIB file.
And give a cell reuse identifier like

This is working for me well for the sample application with no issue with the keyboard.
来源:https://stackoverflow.com/questions/24540437/uitextfield-in-custom-table-view-cell-no-longer-brings-up-keyboard-ios-7