我已将UITextField放在每个UITableView单元格上。在滚动UITableView单元格时,UITextField上的文本会丢失。我该怎么做才能防止这种情况发生?
答案 0 :(得分:1)
您需要跟踪存储文本字段值的NSMutableArray
。将初始值设为@“”。将文本字段插入单元格时,请从数组中的相应值中获取值。
如果文本字段退出,请执行以下操作以将文本字段值存储在数组中:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
UITableViewCell *cell=(UITableViewCell *)textField.superview;
NSIndexPath *indexPath = [table indexPathForCell:cell];
NSMutableArray *rows=[sectionArray objectAtIndex:[indexPath section]];
NSString *string=[rows objectAtIndex:indexPath.row];
string=textField.text;
[[sectionArray objectAtIndex:[indexPath section]]replaceObjectAtIndex:[indexPath row] withObject:string];
[textField resignFirstResponder];
return YES; }
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myCellId= @"myDateCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellId];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: myCellId];
CGRect textFieldFrame;
CGRect labelFrame;
labelFrame = CGRectMake(cell.frame.origin.x+45, cell.frame.origin.y+3, labelWidth, cell.bounds.size.height-5);
textFieldFrame = CGRectMake(cell.frame.origin.x+labelWidth+15, cell.frame.origin.y+12 ,cell.
UILabel *label = [[UILabel alloc]initWithFrame:labelFrame];
UITextField *textField = [[UITextField alloc]initWithFrame:textFieldFrame];
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.delegate = self;
textField.text = [[sectionArray objectAtIndex:[indexPath section]]objectAtIndex:[indexPath row]];
[cell addSubview:label];
[cell addSubview:textField];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
答案 1 :(得分:0)
这是因为当您滚动时,tableview会动态创建和销毁单元格以保存在内存中。如果你还没有找到解决这个问题的方法,请告诉我,我会帮你找到解决方案