我有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];
答案 0 :(得分:1)
您的代码似乎有两个问题:
[dict valueForKey:@"Duration"]
[dict valueForKey:@"Duration"]
实际上没有值,这就解释了为什么一旦再次调用代码,文本字段就会为空。我认为你应该创建一个简单的类,它具有重新定义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
方法中。