Iphone UITextField随机重用UITextField中的文本
这是这个问题的延伸: 我的表在每个单元格中都有UITextFields,因此出现了一些问题。我注意到文本仍然在错误的单元格中被随机重复使用,尽管我似乎正确地保存了它Iphone UITextField随机重用UITextField中的文本,iphone,objective-c,cocoa-touch,uitableview,uitextfield,Iphone,Objective C,Cocoa Touch,Uitableview,Uitextfield,这是这个问题的延伸: 我的表在每个单元格中都有UITextFields,因此出现了一些问题。我注意到文本仍然在错误的单元格中被随机重复使用,尽管我似乎正确地保存了它 else if (tableView == self.vitalsTableView) { if ([indexPath row] == 2) { CellIdentifier = @"bloodPressureCell"; BloodPressureTabl
else if (tableView == self.vitalsTableView)
{
if ([indexPath row] == 2) {
CellIdentifier = @"bloodPressureCell";
BloodPressureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textField1.text = [self.vitalsDictionary objectForKey:@"blood_pressure_1"];
cell.textField2.text = [self.vitalsDictionary objectForKey:@"blood_pressure_2"];
self.bloodPressureTextField1 = cell.textField1;
self.bloodPressureTextField2 = cell.textField2;
return cell;
}
else if ([indexPath row] == 9){
CellIdentifier = @"statusCell";
SmokingStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
else if ([indexPath row] == 10){
CellIdentifier = @"vitalsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
else{
CellIdentifier = @"textCell";
VitalsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch (indexPath.row) {
case 0:
cell.vitalsLabel.text = @"Temperature";
cell.textField.text = [self.vitalsDictionary objectForKey:@"temperature"];
self.temperatureTextField = cell.textField;
break;
case 1:
cell.vitalsLabel.text = @"Pulse";
cell.textField.text = [self.vitalsDictionary objectForKey:@"pulse"];
self.pulseTextField = cell.textField;
break;
case 3:
cell.vitalsLabel.text = @"Respiratory Rate";
cell.textField.text = [self.vitalsDictionary objectForKey:@"respiratory_rate"];
self.respiratoryRateTextField = cell.textField;
break;
case 4:
cell.vitalsLabel.text = @"Oxygen Saturation";
cell.textField.text = [self.vitalsDictionary objectForKey:@"oxygen_saturation"];
self.oxygenSaturationTextField = cell.textField;;
break;
case 5:
cell.vitalsLabel.text = @"Height";
cell.textField.text = [self.vitalsDictionary objectForKey:@"height"];
self.heightTextField = cell.textField;
break;
case 6:
cell.vitalsLabel.text = @"Weight";
cell.textField.text = [self.vitalsDictionary objectForKey:@"weight"];
self.weightTextField = cell.textField;
break;
case 7:
cell.vitalsLabel.text = @"BMI";
cell.textField.text = [self.vitalsDictionary objectForKey:@"bmi"];
self.bmiTextField = cell.textField;
break;
case 8:
cell.vitalsLabel.text = @"Pain (1-10)";
cell.textField.text = [self.vitalsDictionary objectForKey:@"pain"];
self.painTextField = cell.textField;
break;
default:
break;
}
return cell;
}
- (void) textFieldDidEndEditing:(UITextField *)textField{
if (textField == temperatureTextField){
[self.vitalsDictionary setObject:self.temperatureTextField.text forKey:@"temperature"];
}
else if (textField == pulseTextField){
[self.vitalsDictionary setObject:self.pulseTextField.text forKey:@"pulse"];
}
else if (textField == respiratoryRateTextField){
[self.vitalsDictionary setObject:self.respiratoryRateTextField.text forKey:@"respiratory_rate"];
}
else if (textField == oxygenSaturationTextField){
[self.vitalsDictionary setObject:self.oxygenSaturationTextField.text forKey:@"oxygen_saturation"];
}
else if (textField == heightTextField){
[self.vitalsDictionary setObject:self.heightTextField.text forKey:@"height"];
}
else if (textField == weightTextField){
[self.vitalsDictionary setObject:self.weightTextField.text forKey:@"weight"];
}
else if (textField == bmiTextField){
[self.vitalsDictionary setObject:self.bmiTextField.text forKey:@"bmi"];
}
else if (textField == painTextField){
[self.vitalsDictionary setObject:self.painTextField.text forKey:@"pain"];
}
else if (textField == bloodPressureTextField1){
[self.vitalsDictionary setObject:self.bloodPressureTextField1.text forKey:@"blood_pressure_1"];
}
else if (textField == bloodPressureTextField2){
[self.vitalsDictionary setObject:self.bloodPressureTextField2.text forKey:@"blood_pressure_2"];
}
}
不能通过执行textField==pulseTextField进行检查,因为只要重用标识符匹配,UITableview将以不可预测的方式重用单元格。您正在存储对文本字段的引用,但当您上下滚动时,该单元格可能会被分配一个不同的索引。因此,您可能最终会重复引用同一文本字段,因为某些单元格可能会在屏幕外使用过时的重复引用。除了@Barum关于如何访问文本字段的正确答案外,您在创建单元格时也会遇到问题。您正在对缓存的单元格进行出列,但如果出列返回nil,我看不到您的代码创建新单元格。您需要执行以下操作:
else if (tableView == self.vitalsTableView) {
if ([indexPath row] == 2) {
CellIdentifier = @"bloodPressureCell";
BloodPressureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// create cell if nothing dequeued
cell = [[BloodPressureTableViewCell alloc] init...]; // use an appropriate initializer
cell.textField1.tag = MyTextFieldTagBloodPressure1;
cell.textField2.tag = MyTextFieldTagBloodPressure2;
}
// configure the cell
cell.textField1.text = [self.vitalsDictionary objectForKey:@"blood_pressure_1"];
cell.textField2.text = [self.vitalsDictionary objectForKey:@"blood_pressure_2"];
return cell;
}
// do the same for all cell types
注意:上面的示例假设您为标记值定义了一个枚举。我建议这样做,因为这样可以使代码更具可读性,而且您不必记住哪个整数是哪个字段类型。一个选项是利用标签,一个UIView属性。这样,UITextField将附加一个标记,以便您可以识别它的用途。请注意,标记只是NSInteger,所以您可能应该首先声明一些常量。这听起来很完美,您能帮我解决这个问题吗,因为我以前从未使用过常量?我有11个单元格,如示例代码所示。我在思考,而不是设置self.temperatureTextField=cell.textField;,会执行self.temperatureTextField.tag=1或其他操作吗similar@Barum如果正确,则应使用标记访问文本字段。使用cell.textField.tag=1;创建单元格时。确保对单元格中可能的每种类型的文本字段使用唯一的整数。在textfielddidediting中:使用if textField.tag==tag\u值检查特定类型的textField。我看到了另一个问题,我将在单独的答案中提出。