【问题标题】:UITextFields inside UITableViewCells - text disappears when cells go out of viewUITableViewCells 中的 UITextFields - 当单元格消失时,文本会消失
【发布时间】:2013-04-01 10:46:19
【问题描述】:

我有一个UITableView 和一堆UITableViewCells。这些UITableViewCells 内部有多个UITextFields。

现在,每次我上下滚动,UITableViewCells 退出视图,然后返回,我在 UITextField 中输入的任何文本都会消失。完成这项工作的最佳方法是什么?

static NSString *CellIdentifier = @"Cell";

ComplaintsCustomCell *cell=(ComplaintsCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil){
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ComplaintsCustomCell" owner:self options:nil];

    for(id currentObject in topLevelObjects){

        if([currentObject isKindOfClass:[UITableViewCell class]]){
            cell = (ComplaintsCustomCell *) currentObject;
            break;
        }
    }
}

cell.durationFld.text=[dict valueForKey:@"Duration"];
[cell.durationFld setTag:indexPath.row + 5000];

【问题讨论】:

  • 放置cellForRowAtindexpath的代码
  • 我认为你已经在 if (cell == nil) 条件之外编写了文本字段分配代码
  • 检查一下您的 cellIdentifier。
  • 你能在这里发布 cellForRowAtIndexPath 代码吗?我认为重用问题
  • 你从哪里得到那个版本的 loadNibNamed?这似乎不是 Apple 标准问题。

标签: ios objective-c uitableview uitextfield


【解决方案1】:

您的代码似乎有两个问题:

  1. 您将所有单元格的文本字段设置为与 [dict valueForKey:@"Duration"] 完全相同的值
  2. [dict valueForKey:@"Duration"] 似乎实际上没有任何值,这就解释了为什么再次调用您的代码时文本字段为空的原因。

我认为您应该创建一个简单的类,该类具有重新规划 NSArray 的属性并使用文本字段的初始值对其进行初始化。每当文本字段更改时,只需将 indexPath.row 相同索引处的数组对象替换为新文本即可。在您原来的 cellForRowAtIndexPath 方法中,您应该像这样分配文本字段的文本:

cell.durationFld.text = [valuesArray objectAtIndex:indexPath.row];

如果您处理大量文本字段,我还强烈建议您查看免费的 Sensible TableView 框架。祝你好运!

【讨论】:

    【解决方案2】:

    创建一个NSMutableArray,它在每个位置都包含一个与每个单元格匹配的NSString 对象。

    当您配置 + 显示- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中的每个单元格时,将 UITextField 的文本设置为数组中 indexPath.row 位置处的字符串。

    在编辑UITextField 时,在数组中的当前indexPath.row 位置插入/更新NSString 对象

    【讨论】:

      【解决方案3】:

      一般建议:

      UITextField 放入/创建cellForRowAtIndexPath 方法并添加

          [cell.contentView addSubview:textFieldName]
      

      这段代码写在

      if(cell == nil)
       { 
           // put UITextField here
       }
      

      cellForRowAtIndexPath 方法中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 2012-11-01
        • 2017-05-02
        • 1970-01-01
        • 2010-12-09
        • 2022-06-16
        相关资源
        最近更新 更多