[英]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.