2017-08-15 72 views
-1

我正在使用UITableviewCell在我的应用程序项目中创建配置文件表单。所以为此我使用了一个单元。自定义单元由UITextField组成。最初这些字段被填充了Web服务。UITextField在“自定义表格”视图中的值在向上滚动和向下滚动时发生更改

Initial form details

现在我可以编辑的全名,如下图所示:

enter image description here

你可以看到我已经在全名字段中键入劳尔。

但是,如果我向上滚动表格视图并向下滚动,我失去了我更改的值,并显示第一个文本字段中的旧条目。

我使用下面的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // create a new cell if needed or reuse an old one 
    let regCell = profileManagement.dequeueReusableCell(withIdentifier:"regFirstCell", for: indexPath)as? RegistrationFieldTableViewCell 
    regCell?.registrationField.tag = indexPath.row 
    regCell?.registrationFieldImage.image = fieldImages[indexPath.row] 
    let nameFields = NSLocalizedString(regFieldsNames[indexPath.row], comment: "") 
    regCell?.registrationField.placeholder = nameFields 
    regCell?.registrationField.autocorrectionType = .no 
    regCell?.registrationField.keyboardType = UIKeyboardType.default 
    regCell?.showPassword.isHidden = true 
    //Swith case for giving keypad to textfields 
    switch indexPath.row { 
    case 0: 
     regCell?.registrationField.text = self.name 
    case 1: 
     regCell?.registrationField.text = self.emailId 
    case 2: 
     regCell?.registrationField.text = self.civilId 
     regCell?.registrationField.isUserInteractionEnabled = false 
    case 3: 
     regCell?.registrationField.text = self.phoneNumber 
     regCell?.registrationField.isUserInteractionEnabled = false 
    case 4: 
     regCell?.registrationField.isSecureTextEntry = true 
     regCell?.registrationField.text = "abhijith123" 
     regCell?.showPassword.isHidden = true 
     regCell?.registrationField.isUserInteractionEnabled = false 
    default: 
     print("Something else") 
    } 
    return regCell! 
} 
+0

当你输入一个新的值时,你用什么代码来保存它? –

+0

目前我没有使用任何代码来存储新的值。请建议这个怎么办 –

回答

1

想一想。每当我们看到排0时,你说:

regCell?.registrationField.text = self.name 

但是你从来没有变化self.name到什么领域的用户类型。所以很自然地,当我们再次显示0行时,它会回到旧的self.name

+0

是的,当然。因此,请在我们编辑文本字段中的值时建议执行什么操作 –

+1

使您自己成为文本字段的操作目标,以便获取“editingChanged”控制事件。或者是文本字段的委托,以便获得'textFieldDidEndEditing'。当您收到活动时,请阅读文本字段的值并将其存储在适当的位置。示例在这里:https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p447editInsertAndRearrangeRows/ch21p741editInsertAndRearrangeRows/RootViewController.swift – matt

+0

我可以看到editingChanged的代码。这是我们唯一需要使用的东西吗? –

3

这是由于细胞的可重用性。

您需要保存所做的更改才能在下次查看单元格时调出它们。目前你正在做的是你正在改变文本,擦去(单元格被重用)。然后向后滑动(单元格重新加载cellForRowAt函数,因此它的值被设置)

您需要做的是实际存储用户插入的值,以便当用户滚动后再返回,我们显示保存的值。

要做到这一点,存储在数组中你的价值观,并让yourlabel.text = yourArray[indexPath.row]

因此,假如你有3行的实现代码如下,每一行都有一个TextView。在cellAtIndex功能

3-在cellAtIndex功能设置textField.text = valuesArray[indexPath.row]以及

4-在你didEndEditing代表为TextField valuesArray = ["", "", ""]

2-组textField.tag = indexPath.row

1-定义的阵列,设置为:valuesArray[textField.tag] = textField.text

而且这应该工作

希望这有助于!

+0

@matt谢谢我刚刚做到了! –

+0

@MohammadBashirSidani请在您的答案中插入代码以存储用户正在插入的值 –

+0

@ChelseaShawra我更新了我的代码,请检查 –

相关问题