UITextFields在UITableViewCells中 - 当单元格离开视图时,文本消失

时间:2013-04-01 10:46:20

标签: ios objective-c uitableview uitextfield

我有UITableView一堆UITableViewCell个。这些UITableViewCell内有多个UITextField

现在每次我上下滚动,UITableViewCell走出视图,然后回来,我在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];

3 个答案:

答案 0 :(得分:1)

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

  1. 您将所有单元格的文本字段设置为完全相同的[dict valueForKey:@"Duration"]
  2. 看来[dict valueForKey:@"Duration"]实际上没有值,这就解释了为什么一旦再次调用代码,文本字段就会为空。
  3. 我认为你应该创建一个简单的类,它具有重新定义NSArray的属性,并使用文本字段的初始值初始化它。每当文本字段发生更改时,只需使用新文本替换indexPath.row的相同索引处的数组对象。在原始的cellForRowAtIndexPath方法中,您应该像这样分配文本字段的文本:

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

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

答案 1 :(得分:0)

创建一个NSMutableArray,在每个位置保存一个匹配每个单元格的NSString对象。

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

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

答案 2 :(得分:-1)

一般建议:

UITextField方法中添加/创建cellForRowAtIndexPath并添加

    [cell.contentView addSubview:textFieldName]

此代码在

之间写入
if(cell == nil)
 { 
     // put UITextField here
 }

cellForRowAtIndexPath方法中。