繁体   English   中英

UITableViewCells内部的UITextFields-当单元格不在视野中时,文本消失

[英]UITextFields inside UITableViewCells - text disappears when cells go out of view

我有一个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];

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

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

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

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

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

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

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

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

一般建议:

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

    [cell.contentView addSubview:textFieldName]

这段代码之间写

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

cellForRowAtIndexPath方法中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM