简体   繁体   中英

UITextField inside UITableViewCell gets erased on scroll

I have a small form (5 fields) in a grouped UITableView. Each UITextField is inside of a UITableView cell. When clicking in a textField, it brings up the keyboard, which then allows you to scroll some of the cells out of view, when you pull them back down, their content is erased.

Am I correct in assuming that they have been redrawn, so their content is gone? If so, what is the best way to prevent this? Since I only have 5 fields, do I have to redraw my cells when the are scrolled back into view?

I am reusing cells:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

    }

You can have a separate array of you text fields and implement you cell creation callback like this:

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath 
{
   static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

    }
    [[cell.contentView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];

    [cell.contentView addSubview: [textFieldsArray objectsAtIndex: indexPath.row]];

    return cell;

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM