[英]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];
您的代码似乎有两个问题:
[dict valueForKey:@"Duration"]
完全相同的值 [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.