【问题标题】:Textfield value repeating in UITableView while scrolling滚动时在 UITableView 中重复的文本字段值
【发布时间】:2019-06-23 08:11:57
【问题描述】:

类似的问题已经被问过很多次,并且提到了很多解决方案,但没有一个对我有用,因为它们解决了固定数量的单元格,而我有动态数量的单元格。问题来了:

它在 Objective-C 中。我有 动态行数的 tableview(用户可以根据需要添加更多单元格或删除单元格) 并且 每行包含 3 个文本字段,最后我想从该文本字段中获取数据并保存它们。从所有文本字段中获取数据都运行良好,但是当我将数据添加到任何行(3 个文本字段)中时,主要问题出现了,当我滚动它时它会重复。

代码如下:

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([_txtNumbers resignFirstResponder]) {
        totalIndex = [_txtNumbers.text integerValue];
        [_tblContacts reloadData];
    }
}

- (IBAction)onAddCellClicked:(id)sender {
    totalIndex = [_txtNumbers.text integerValue];
    totalIndex = totalIndex + 1;
    _txtNumbers.text = [NSString stringWithFormat:@"%ld",(long)totalIndex];
    [_tblContacts reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return totalIndex;
}

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

    static NSString *CellIdentifier = @"InviteEmailTableViewCell";

    InviteEmailTableViewCell *cell = [self.tblContacts dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.txtName.delegate = self;
    cell.txtTell.delegate = self;
    cell.txtEmail.delegate = self;

    UIToolbar  *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    [numberToolbar setTintColor:kShadowColor1];
    [numberToolbar setBarTintColor:[UIColor blackColor]];
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
                           nil];
    cell.txtTell.inputAccessoryView = numberToolbar;

    [cell.txtName setTag:indexPath.row];
    [cell.txtTell setTag:indexPath.row];
    [cell.txtEmail setTag:indexPath.row];

    return cell;
}

接下来该做什么?

【问题讨论】:

  • 在您的自定义单元格类中覆盖tableview单元格的perpareForReuse方法,并在此方法中重置文本字段错误。
  • @vivekDas 感谢您的回复,这是在 objC 中,我尝试覆盖它但没有成功,如果您能告诉我如何在 ObjC 中做这将是很大的帮助。
  • 到目前为止您为 perpareForReuse 所尝试的内容显示代码。

标签: ios objective-c uitableview uitextfield


【解决方案1】:

正如其他人所提到的,您的表格视图单元格正在被重用,文本字段也是如此。由于这些文本字段不会被破坏和重新创建,因此您必须手动重置这些字段的状态。

Your datasource should be responsible for managing the view state.

正如 Mehul 在他的回答中提到的,保留一个数据源来存储在文本字段中键入的文本(使用文本字段标签作为 indexPath.row 可能会有所帮助),并在 cellForRowAtIndexPath 中使用您的数据源更新这些文本字段的文本。

这种方法可确保您的视图和数据源之间的同步。

【讨论】:

  • 是的,我明白这一点,但情况是我在单元格中有 3 个文本字段,单元格的数量可以增加或减少。
  • @Mr.Desai :您可以根据您正在增加或减少您的单元格来增加或减少(更新)数组。
【解决方案2】:

随着文本字段的重复使用,很明显输入的值将被重复。

解决办法:

  1. 创建一个数组来保存每个单元格/行的数据(行数 = array.count)。
  2. 保留每个文本字段的委托 - 在每个文本字段的 textFieldDidEndEditing 方法上,将数据保存在数组中。
  3. cellForRowAtIndexPath 方法中,对于每个单元格的每个文本字段,从给定索引路径的数组中获取数据并为该文本字段设置相关值。

【讨论】:

  • 谢谢你的回答,但我仍然感到困惑,因为我尝试了很多事情,所以可能是因为混乱,我无法注意你到底在说什么。如果可能的话,你能告诉我要遵循的步骤吗?我知道你已经写了步骤,但我不清楚。
  • 首先了解你的问题,为什么你会遇到这个问题。你知道'dequeueReusableCellWithIdentifier'这一行的含义吗?它只是意味着 - 将要隐藏的单元格将再次被重用。因此,很明显,您已经填写的文本字段值将被重复。
  • 所以,你不能依赖 tableview 来存储你的数据。所以,你必须创建一个数组来保存数据。
  • 好的,我明白了。所以我应该将数据保存到 textFieldDidEndEditing 中的数组中吗?你提到将委托添加到 textFieldDidEndEditing 那么我应该从 cellForRowAtIndexPath 中删除它吗?
  • 当前委托非常好.. 只是告诉你 - 1. 每当任何 textfild 的值发生更改时 - 将新值保存到数组中,2. 在 cellForRowAtIndexPath 方法中,从数组中获取相应的值并设置为相关文本字段。
猜你喜欢
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
相关资源
最近更新 更多