如果一个文本字段已添加到单元格中,如何不创建新的文本字段 [英] How NOT to create a new textfield if one textfield has been added to the cell
问题描述
我正在使用以下代码在 UITableView 单元格中创建文本字段:
I'm using the following code to create the textfield in a UITableView Cell:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.showsReorderControl = YES;
}
if (indexPath.row == 1)
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15,10,260,40)];
textField.placeholder = @"Activity 1: Type Name";
textField.delegate = self;
textField.clearButtonMode = YES;
[textField setReturnKeyType:UIReturnKeyDone];
[cell addSubview:textField];
}
return cell;
我正在以完全相同的方式创建 3 个 textField.唯一的区别是 placeHolder 文本.
I'm creating 3 textFields the exact same way. The only difference is the placeHolder text.
当键盘弹出时,视图向上滚动并且 textField 1 离开屏幕.返回后,我认为正在重新创建 textField.
When the keyboard pops up, the view scrolls up and the textField 1 goes off the screen. Upon return, I think the textField is being recreated.
以下是一些屏幕截图:
第一次出现单元格(看起来很棒):
First time appearance of the cell (looks great):
滚出屏幕后返回(注意第一个文本框):
Returns after being scrolled off the screen (notice the first textfield):
在第一个单元格中,当我开始输入时,第二个创建的 textField 的占位符消失了,但第一个 textField 的占位符仍然存在:
In cell one, when I start typing, the 2nd created textField's placeHolder disappears, but the first textField's Placeholder remains:
两个问题:
- 如何避免在单元格中重新创建 textField?或者消除这个问题?
- 重新创建时,为什么单元格 3 中的 textField 和 placeHolder 为Activity 3: Type Name"出现在单元格 1 的 textField 上?
推荐答案
我假设这段代码在 cellForRow
tableview dataSource 协议方法中.问题是此方法被多次调用(有时在您意想不到的情况下),导致创建一个新的文本字段并将其添加到同一个单元格中.为了解决这个问题你只需要在创建单元格时添加一个文本字段,然后在每次调用方法时配置单元格.我建议创建一个表格单元子类,但您可以将代码更改为此进行学习:
I am assuming this code is in the cellForRow
tableview dataSource protocol method. The problem is that this method is called multiple times (sometimes when you wouldn't expect), causing a new text field to be created and added to the same cell. In order to resolve this issue you need to add a text field only when the cell is created, and then configure the cells each time the method is called. I would recommend creating a table cell subclass, but you can change your code to this for learning purposes:
#define kTextFieldTag 1
UITextField* textField = [cell viewWithTag:kTextFieldTag];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.showsReorderControl = YES;
/* only called when cell is created */
textField = [[UITextField alloc] initWithFrame:CGRectMake(15,10,260,40)];
textField.delegate = self;
textField.clearButtonMode = YES;
textField.tag = kTextFieldTag; /* I would recommend a cell subclass with a textfield member over the tag method in real code*/
[textField setReturnKeyType:UIReturnKeyDone];
[cell addSubview:textField];
}
/* called whenever cell content needs to be updated */
if (indexPath.row == 1)
{
textField.placeholder = @"Activity 1: Type Name";
}
...
/* or replace if checks with with: */
textField.placeholder = [NSString stringWithFormat:@"Activity %i: Type Name", (int)indexPath.row]; /* Handles all fields :) */
...
这篇关于如果一个文本字段已添加到单元格中,如何不创建新的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!