在自定义单元格(xib)中滚动时,UITextField 中的文本消失

Posted

技术标签:

【中文标题】在自定义单元格(xib)中滚动时,UITextField 中的文本消失【英文标题】:Text in UITextField disappear when scrolling in custom cell (xib) 【发布时间】:2013-05-02 06:24:55 【问题描述】:

我已经阅读了很多关于我的问题的信息,但我仍然无法指出确切的问题。 如果代码在堆栈中,我已经尝试了一些,但我不断收到错误。 下面的代码有效,但滚动时文本消失。

有什么想法吗?

GetQuestionsCustomCell.h:

@interface GetQuestionsCustomCell : UITableViewCell <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UILabel *labelQuestion;
@property (strong, nonatomic) IBOutlet UITextField *textFieldAnswer;
@property (strong, nonatomic) IBOutlet UILabel *labelQuestionNumber;
- (IBAction)KeyDown:(id)sender;

@end

GetQuestionsCustomCell.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
        // Initialization code
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"GetQuestionsCustomCell" owner:self options:nil];
        self = [nibArray objectAtIndex:0];
        self.textFieldAnswer.delegate = self;
    
    return self;

SecurityQuestions.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";

    GetQuestionsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
        cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    


    NSInteger nextIndexPathRow = indexPath.row;
    nextIndexPathRow++;
    cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
    cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];

    switch (indexPath.row) 
        case 0:
            tfA1 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 1:
            tfA2 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 2:
            tfA3 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 3:
            tfA4 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 4:
            tfA5 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 5:
            tfA6 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 6:
            tfA7 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 7:
            tfA8 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 8:
            tfA9 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 9:
            tfA10 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 10:
            tfA11 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 11:
            tfA12 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
    

    return cell;

【问题讨论】:

问题在于单元格可重用.. 当自定义单元格中有 UITextField 时,您只需要知道如何重用单元格.. 这可能对您有帮助 ***.com/a/4568460/1059705 和 ***.com/q/4045658/1059705 我能问你一件事,你想从每个文本字段中获取用户输入吗? 从文本字段中获取每个数据并保存以备后用。 【参考方案1】:

确切的问题是您既没有保存文本字段的值,也没有将它们设置在cellForRowAtIndexPath

当用户在文本字段中输入内容时,您需要将值跟踪到某处,可能在您的 ViewController 中按问题编号索引的数组中。一种方法是跟踪cellForRowAtIndexPath 中的单元格:

cell.textFieldAnswer.tag = indexPath.row;
cell.textFieldAnswer.delegate = self;

然后你需要在你的视图控制器中实现- (void)textFieldDidEndEditing:(UITextField *)textField。将那里的值另存为:

[self.answers replaceObjectAtIndex:textField.tag withObject: textField.text];

然后你需要在你的cellForRowAtIndexPath 中再次将值设置为[cell.textFieldAnswer setText:[self.answers objectAtIndex:indexPath.row]]

原因是,如果你有 1000 个问题,ios 不会创建 1000 个单元格。它将重用单元格。您需要进一步了解 tableView:TableView Programming Guide

希望这会有所帮助。

【讨论】:

处理..的确切方法:) 好的,谢谢您的回答。我想在编辑完成时从文本字段中获取文本,但使用 - (BOOL)textFieldShouldReturn:(UITextField *)textField 和 - (void)textFieldDidEndEditing:(UITextField *)textField 对我来说有点问题,因为我需要设置委托给文本字段,但文本字段在自定义类中,知道我如何处理它吗? 非常感谢!哦,cell.textFieldAnswer = indexPath.row;给我错误“...的隐式转换” 你的意思可能是:cell.textFieldAnswer.tag = indexPath.row; 第一次不工作。它崩溃了。第一次数组呢。【参考方案2】:

更改 CellForRowIndexPath...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";

    GetQuestionsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
        cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    switch (indexPath.row) 
        case 0:
            tfA1 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 1:
            tfA2 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 2:
            tfA3 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 3:
            tfA4 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 4:
            tfA5 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 5:
            tfA6 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 6:
            tfA7 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 7:
            tfA8 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 8:
            tfA9 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 9:
            tfA10 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 10:
            tfA11 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 11:
            tfA12 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
    


    NSInteger nextIndexPathRow = indexPath.row;
    nextIndexPathRow++;
    cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
    cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];

[tfA1 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA2 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA3 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA4 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA5 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA6 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA7 setText:[self.answers objectAtIndex:indexPath.row]];
  
   return cell;

【讨论】:

【参考方案3】:

这是一个使用tableView delegate method 的简单解决方法,并在此方法中保存您的UITextview.text 值,

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath


//Save the textField values


【讨论】:

以上是关于在自定义单元格(xib)中滚动时,UITextField 中的文本消失的主要内容,如果未能解决你的问题,请参考以下文章

用于 UITableView 的自定义 XIB 单元格在滚动时卡住/卡住

在自定义 UICollectionView 中出列时返回错误的单元格

自定义tableviewcell中的文本字段在滚动时离开单元格

UIButton 在自定义单元格中更改

ag-grid:水平向后和向前滚动后,在自定义单元格渲染器中发现服务未定义

图像的滚动视图在自定义表格视图单元格中不起作用