UITableViewCell中的UITextField在滚动时被删除

Nic*_*ard 2 iphone uitableview uitextfield ios

我在一个分组的UITableView中有一个小表单(5个字段).每个UITextField都在UITableView单元格内.当单击textField时,它会弹出键盘,然后允许您将一些单元格滚出视图,当您将它们拉回时,它们的内容将被删除.

假设它们已被重绘,我是否正确,所以它们的内容已经消失了?如果是这样,防止这种情况的最佳方法是什么?由于我只有5个字段,当滚动回到视图时,是否必须重绘我的单元格?

我正在重复使用细胞:

static NSString *CellIdentifier = @"Cell";

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

    }
Run Code Online (Sandbox Code Playgroud)

Max*_*Max 5

您可以拥有单独的文本字段数组,并实现单元格创建回调,如下所示:

-(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;

} 
Run Code Online (Sandbox Code Playgroud)