【问题标题】:ios tableview text edit data lost when scrollingios tableview文本编辑滚动时数据丢失
【发布时间】:2024-05-19 17:20:02
【问题描述】:

我正在使用 UIview 并添加了 tableview。API 发送数据以在 tableiveiw 中显示文本字段。我可以使用编码在单元格中显示文本字段数据 bt 问题是当我滚动 tableview 时文本值丢失。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return subIncidentArray.count;
}

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

    static NSString *cellIdentifier = @"cellapi";
    UITableViewCell *incidentCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSInteger row = [indexPath row];
    NSDictionary *dic = [subIncidentArray objectAtIndex:row];

    if (incidentCell == nil)
        incidentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    if ([[dic objectForKey:@"TemplateType"] isEqual:@1]) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 300, 30)];
        textField.delegate = self;
        textField.tag = 1;
        textField.text = self.contentOfTextField;
        [incidentCell addSubview:textField];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.textColor = [UIColor grayColor];

        if ([dic objectForKey:@"TextValue"] ==[NSNull null])
             textField.text = [dic objectForKey:@""];
        else
             textField.text = [dic objectForKey:@"TextValue"];
    }

    return incidentCell;
}

【问题讨论】:

  • 那是因为单元格被重复使用并且您正在重新创建文本字段。如果您确实希望用户在表格视图单元格中输入文本,请考虑将文本保存在数组中,然后在加载该索引时重新填充文本字段。
  • 感谢您的快速回复。请显示一些示例代码。我是IOS应用程序开发的新手..
  • 当您说“文本值丢失”时,您是指编辑文本后的值吗?还是初始值?

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

我遇到了同样的错误,首先请确保您的subIncidentArraycellForRowAtIndexPath 中不为空。 尝试在这个方法中重新初始化这个数组的数据

【讨论】:

    【解决方案2】:
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
         UITableViewCell *incidentCell = [UITableViewCell alloc]init];
         NSInteger row = [indexPath row];
         NSDictionary *dic = [subIncidentArray objectAtIndex:row];
         if ([[dic objectForKey:@"TemplateType"]  isEqual:@1]) {
             UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 300, 30)];
             textField.delegate = self;
             textField.tag = 1;
             textField.text = self.contentOfTextField;
             [incidentCell addSubview:textField];
             textField.borderStyle = UITextBorderStyleRoundedRect;
             textField.textColor = [UIColor grayColor];
             if ([dic objectForKey:@"TextValue"] ==[NSNull null])
                textField.text = [dic objectForKey:@""];
             else
                textField.text = [dic objectForKey:@"TextValue"];
         }
         return incidentCell;
    }
    

    试试这个可能有用。

    【讨论】:

    • 不重用单元格会影响性能,尤其是在数据源很大的情况下。另外,您的代码有两个返回值,应该只有一个返回值,即incidentCell
    • @mahesh 抱歉不工作...返回单元格不在我的代码中
    【解决方案3】:

    这样试试,可能会成功

      UITableViewCell *incidentCell = [[UITableViewCell alloc]init];
    

    【讨论】:

    • 确定我下次会这样做