How NOT to create a new textfield if one textfield has been added to the cell

可紊 提交于 2019-12-25 11:50:52

问题


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;

I'm creating 3 textFields the exact same way. The only difference is the placeHolder text.

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.

Below are some screen shots:

First time appearance of the cell (looks great):

Returns after being scrolled off the screen (notice the first textfield):

In cell one, when I start typing, the 2nd created textField's placeHolder disappears, but the first textField's Placeholder remains:

Two questions:

  1. How do I avoid recreating textField in the cell? or eliminate this problem?
  2. When recreated, why is textField from Cell 3 with placeHolder of "Activity 3: Type Name" appears on cell one textField?

回答1:


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 :) */
...



回答2:


I also recommend you check out the free Sensible TableView framework. The framework not only automatically creates the input cells for you, but will also auto apply the changes back to your objects. Highly recommended and saves me tons of time.



来源:https://stackoverflow.com/questions/17560209/how-not-to-create-a-new-textfield-if-one-textfield-has-been-added-to-the-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!