TableViewCell 内的 UItextField 包含多个部分 - 数据重复
Posted
技术标签:
【中文标题】TableViewCell 内的 UItextField 包含多个部分 - 数据重复【英文标题】:UItextField inside TableViewCell with more than one section - data duplicate 【发布时间】:2017-05-20 09:23:38 【问题描述】:我有一个表格视图来复制表单页面。它有两个部分,第 0 部分有 8 个单元格,其他有 4 个单元格
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return [tableViewSectionData count];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
switch (section)
case 0:
return 8;
break;
case 1:
return 4;
break;
default:
return 0;
break;
一些单元格用于显示选择器,我通过在单元格内的文本字段上添加一个按钮并在必要时启用然后启用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if (indexPath.section == 0)
TextFieldCell *cellInSection0 = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCellInSection0" forIndexPath:indexPath];
if (!cellInSection0)
cellInSection0 = [[TextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCellInSection0"];
cellInSection0.bottomButton.hidden = YES;
cellInSection0.textFieldBottomLabel.alpha = 0.5;
cellInSection0.textFieldImageView.alpha = 0.5;
cellInSection0.textfield.delegate = self;
cellInSection0.textfield.tag = indexPath.row;
cellInSection0.pickerViewButton.hidden = YES;
NSString *placeholderString;
NSString *imageName;
switch (indexPath.row)
case 0:
if (fname)
cellInSection0.textfield.text = fname;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
else
placeholderString = @"FIRST NAME";
imageName = @"user";
break;
case 1:
if (lname)
cellInSection0.textfield.text = lname;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
else
placeholderString = @"LAST NAME";
imageName = @"user";
break;
case 2:
if (address)
cellInSection0.textfield.text = address;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
else
placeholderString = @"ADDRESS";
imageName = @"address";
break;
case 3:
cellInSection0.textfield.returnKeyType = UIReturnKeyDone;
if (floor_unit)
cellInSection0.textfield.text = floor_unit;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
else
placeholderString = @"FLOOR/UNIT";
imageName = @"address";
break;
case 4:
cellInSection0.pickerViewButton.hidden = NO;
cellInSection0.textfield.enabled = NO;
cellInSection0.pickerViewButton.tag = indexPath.row;
[cellInSection0.pickerViewButton addTarget:self action:@selector(showCountryPicker:) forControlEvents:UIControlEventTouchUpInside];
if (countryName)
cellInSection0.textfield.text = countryName;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
cellInSection0.textfield.alpha = 1.0;
else
cellInSection0.textfield.text = @"COUNTRY";
cellInSection0.textfield.alpha = 0.5;
imageName = @"mapicon";
break;
case 5:
cellInSection0.pickerViewButton.hidden = NO;
cellInSection0.textfield.enabled = NO;
cellInSection0.pickerViewButton.tag = indexPath.row;
[cellInSection0.pickerViewButton addTarget:self action:@selector(showStatePicker:) forControlEvents:UIControlEventTouchUpInside];
if (stateName)
cellInSection0.textfield.text = stateName;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
cellInSection0.textfield.alpha = 1.0;
else
cellInSection0.textfield.text = @"STATE";
cellInSection0.textfield.alpha = 0.5;
imageName = @"mapicon";
break;
case 6:
cellInSection0.pickerViewButton.hidden = NO;
cellInSection0.textfield.enabled = NO;
cellInSection0.pickerViewButton.tag = indexPath.row;
[cellInSection0.pickerViewButton addTarget:self action:@selector(showCityPicker:) forControlEvents:UIControlEventTouchUpInside];
if (cityName)
cellInSection0.textfield.text = cityName;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
cellInSection0.textfield.alpha = 1.0;
else
cellInSection0.textfield.text = @"CITY";
cellInSection0.textfield.alpha = 0.5;
imageName = @"mapicon";
break;
case 7:
placeholderString = @"ZIP";
imageName = @"mapicon";
break;
default:
break;
if (placeholderString)
NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:placeholderString attributes:@NSForegroundColorAttributeName: [UIColor lightGrayColor]];
cellInSection0.textfield.attributedPlaceholder = placeHolder;
cellInSection0.textFieldImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]];
return cellInSection0;
else
TextFieldCell *cellInSection1 = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCellInSection1" forIndexPath:indexPath];
if (!cellInSection1)
cellInSection1 = [[TextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCellInSection1"];
cellInSection1.bottomButton.hidden = YES;
cellInSection1.textFieldBottomLabel.alpha = 0.5;
cellInSection1.textFieldImageView.alpha = 0.5;
cellInSection1.textfield.delegate = self;
cellInSection1.textfield.tag = indexPath.row;
NSString *placeholderString;
NSString *imageName;
switch (indexPath.row)
case 0:
if (cardNumber)
cellInSection1.textfield.text = cardNumber;
else
placeholderString = @"CARD NO.";
imageName = @"yearofincorporationicon";
break;
case 1:
if (month)
cellInSection1.textfield.text = month;
else
placeholderString = @"MONTH";
imageName = @"calender";
break;
case 2:
if (year)
cellInSection1.textfield.text = year;
else
placeholderString = @"YEAR";
imageName = @"calender";
break;
case 3:
if (cvv)
cellInSection1.textfield.text = cvv;
else
placeholderString = @"CVV";
imageName = @"yearofincorporationicon";
break;
default:
break;
if (placeholderString)
NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:placeholderString attributes:@NSForegroundColorAttributeName: [UIColor lightGrayColor]];
cellInSection1.textfield.attributedPlaceholder = placeHolder;
cellInSection1.textFieldImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]];
return cellInSection1;
乍一看,这很有效。但是当我在文本字段中输入一些文本(例如第 0 节中的文本字段第一个)时,第 1 节的第一个文本字段也显示相同的文本。这发生在每个文本字段中。有时占位符字符串也不匹配。它发生得如此随机,我无法找到问题所在。
任何帮助都很好。 谢谢
【问题讨论】:
【参考方案1】:您需要创建单独的值来跟踪您的文本字段值。以及 viewDidLoad 中的初始值变化
var arrInitialValues: [Any] = ["", "", ""] // this array count should be same as datasource array count
在 cellForRowAtIndexPath 中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
cell.txtfield.text = arrInitialValues[indexPath.row] as! String
在委托方法中替换该值。
func textFieldDidEndEditing(_ textField: UITextField)
let buttonPosition = textField.convertPoint(CGPoint.zero, to: tblView)
let indexPath: IndexPath? = tblView.indexPathForRow(at: buttonPosition)
arrInitialValues[indexPath?.row] = textField.text // or if you use model property then you can change that property here
请注意,这是您可以根据需要修改的示例代码
【讨论】:
好的。我对let indexPath: IndexPath? = tblView.indexPathForRow(at: buttonPosition) arrInitialValues[indexPath?.row] = textField.text
有疑问。我有两个部分,textFieldDidEndEditing
方法将如何使用我当前的文本字段。文本字段的标签只能设置为 indexpath.row(不是直接的 indexpath)。
目前我正在使用[NSIndexPath indexPathForRow:textField.tag inSection:0]
制作一个单元格对象,仅用于第 0 部分。并通过此单元格对象从其文本字段中获取文本。
例如,您在第一部分有八行,然后在 array 中添加八个初始值。那么如果在另一个部分你有 3 行然后添加具有 3 个初始值的新数组。【参考方案2】:
发生这种情况是因为单元格被重复使用。如果您没有正确设置 if-else 条件中的属性值,这是一个常见问题。
例如,在您的第一个 switch
案例 if
条件中,您正在更改 textField
属性,但不是在 else 条件中。
假设满足第一个单元格if
的条件。现在让我们说当单元被重用时,它将进入其他条件。但由于该单元格被重用,它将反映在 if 条件下对第一个单元格所做的更改。由于else
条件不会更改textField
属性,因此您将获得旧值。
我正在更改 case 0
if-else
条件。
if (fname)
cellInSection0.textfield.text = fname;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
placeholderString = @"ifPartPlaceHolderName";
else
cellInSection0.textfield.text = @"Else part name";
cellInSection0.textFieldBottomLabel.alpha = elsePartAlphaValue;
cellInSection0.textFieldImageView.alpha = elsePartAlphaValue;
placeholderString = @"FIRST NAME";
您还需要更改剩余的案例。请记住,无论您在if
条件下更改什么单元格属性,您都必须在else
部分中提供替代选项。
【讨论】:
谢谢!这是有道理的。我会你的建议 没有。它与文本字段中的现有文本重叠。以上是关于TableViewCell 内的 UItextField 包含多个部分 - 数据重复的主要内容,如果未能解决你的问题,请参考以下文章
未调用 TableViewCell 内的 UICollectionView
更改 TableViewCell 内的 UIPageControl 的 CurrentPage 时出现问题
TableViewCell 内的 UItextField 包含多个部分 - 数据重复
无法单击 iOS 14 中 TableViewCell 内的按钮 [重复]