【发布时间】: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