Reputation: 9390
I want to create a text field in the group table view cell. If I create one text field in a cell, the text fields are over lapped in all the grouped table view cells. I don't know how it happened. So I want to create a text field in a grouped table view cell (Not all the cells). How can I achieve this? Is there any sample code available?
Here my sample code,
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
UITextField * lastname = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 150, 35)];
lastname.tag = titleTag2;
[cell.contentView addSubview:lastname];
lastname.delegate = self;
lastname.returnKeyType = UIReturnKeyDefault;
}
}
Upvotes: 2
Views: 17157
Reputation: 3931
just in case anyone comes across this question, this is how you add UITextFields to a group table.
In the UITableViewController (which I assume is the table's delegate & datasource) implement the following functions:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0) {
// Add a UITextField
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = titleTag2 + indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
[textField release];
}
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:titleTag2+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
textField.frame = aRect;
// Configure UITextAttributes for each field
if(indexPath.row == 0) {
textField.placeholder = @"Name";
textField.returnKeyType = UIReturnKeyNext;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
} else if(indexPath.row == 1) {
textField.placeholder = @"Email Address";
textField.returnKeyType = UIReturnKeyNext;
textField.keyboardType = UIKeyboardTypeEmailAddress;
} else {
textField.placeholder = @"Password";
textField.returnKeyType = UIReturnKeyDone;
textField.secureTextEntry = YES;
}
}
}
Obviously, you'll still need to implement the UITextFieldDelegate methods to actually process the text entry and moving the first responder between fields.
UPDATE: Since posting this, I realised that you can use the UIControl contentVerticalAlignment property, and set that to UIControlContentVerticalAlignmentCenter, in which case you can just set your frame to the bounds of the cell's content view.
Upvotes: 29
Reputation: 2210
I think that the Daniel solution is not good in some cases. For example, if tableview section 0 contains two or more rows, dequeueReusableCellWithIdentifier could return a cell with text field tag wrong. In particular, if a cell was put in the queue when cell at indexPath
row 0 was created and dequeueReusableCellWithIdentifier returns this cell when cell at indexPath
row 3 was created, the tag of text field will be wrong.
Upvotes: 0
Reputation: 3517
For good example you can see iPhoneCoreDataRecipes sample. In this sample EditingTableViewCell loaded from nib file, but you can easy construct it in code.
Upvotes: 2