问题描述
这是此问题的扩展:
我的表存在一些问题,该表的每个单元格中都有UITextFields。我注意到即使我似乎正确地保存了文本,文本仍在错误的单元格中被随机重用。
I'm having some issues with my table that has UITextFields in each cell. I notice text is still getting reused randomly in the wrong cells even though I seem to be saving it correctly.
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
将以不可预测的方式重用单元格,只要重用标识符即可比赛。您正在存储对文本字段的引用,但是当您上下滚动时,该单元格可能会被分配一个不同的索引。因此,您可能最终会获得对同一文本字段的重复引用,因为某些单元格可能会在屏幕外出现过时的重复引用。
You cannot check by doing textField == pulseTextField
, because UITableview
will reuse cells in a unpredictable manner as long as the reuse identifiers match. You are storing a reference to a text field, but when you scroll up and down, that cell may get assigned a different index. So, you may end up with duplicate reference to the same text field, since some cells may be off-screen with outdated duplicate reference.
这篇关于UITextField随机重用UITextField中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!