[英]How to create a Text Field in Grouped Table view Cell in iPhone
我想在组表视图单元格中创建一个文本字段。 如果我在单元格中创建一个文本字段,则在所有分组的表格视图单元格中重叠文本字段。 我不知道是怎么回事。 所以我想在分组表视图单元格中创建一个文本字段(不是所有单元格)。 我怎样才能做到这一点? 有可用的示例代码吗?
这是我的示例代码,
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;
}
}
如果有人遇到这个问题,这就是你如何将UITextFields添加到组表中。
在UITableViewController(我假设是表的委托和数据源)中实现以下功能:
- (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;
}
}
}
显然,您仍然需要实现UITextFieldDelegate方法来实际处理文本条目并在字段之间移动第一个响应者。
更新:自发布以来,我意识到您可以使用UIControl contentVerticalAlignment属性,并将其设置为UIControlContentVerticalAlignmentCenter,在这种情况下,您可以将帧设置为单元格内容视图的边界。
例如,您可以看到iPhoneCoreDataRecipes示例。 在此示例中,从nib文件加载EditingTableViewCell,但您可以在代码中轻松构造它。
我认为Daniel解决方案在某些情况下并不好。 例如,如果tableview部分0包含两行或更多行,则dequeueReusableCellWithIdentifier可能会返回文本字段标记错误的单元格。 特别是,如果在创建indexPath
第0行的单元格时将单元格放入队列,并且dequeueReusableCellWithIdentifier在创建indexPath
第3行的单元格时返回此单元格,则文本字段的标记将是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.