Scrollview of UiTableview with multiple textfield shows copy value from one to another

泄露秘密 提交于 2019-12-11 18:11:09

问题


I am facing issue with dynamic cell which create multiple Textfield based on selection of my medicationTime .when i entered select medicationTime based on that my textfields are created and if i deselect medicationTime ,textfield should get removed from uiTableview cell which is working fine.Problem i am facing while scrolling my tableview as 1st textfield value get copied to 5 th textfield and vice versa. Also while deleting textfiled the values get copying

UITableViewDelegate ,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if timeArray.count > 0 {
            return timeArray.count

        } else {
            return 0
        }

    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell: AddMedicationTableViewCell = medicineTimeTableView.dequeueReusableCell(withIdentifier: "AddMedicationTimeCell",for:indexPath as IndexPath) as! AddMedicationTableViewCell
        cell.quantityTextField.delegate = self
        if timeArray.count > 0 {
        cell.timeLabel.text =  timeArray[indexPath.row]
        cell.quantityTextField.keyboardType = .decimalPad
       // cell.quantityTextField.delegate = self

//            let subViews = cell.contentView.subviews
//            for subview in subViews
//            {
//                if (subview is UITextField)
//                {
//                    subview.removeFromSuperview()
//                }
//            }

            if arrayofTextField.indices.contains(indexPath.row)
            {
                cell.quantityTextField = arrayofTextField[indexPath.row ]

            } else {

                cell.quantityTextField.text = dataList[indexPath.row]
                cell.quantityTextField.tag = indexPath.row

                arrayofTextField.append(cell.quantityTextField)
            }




        cell.selectionStyle = .none

          return cell
    }

//Deleting code

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let deSelectedCell = collectionView.cellForItem(at: indexPath)


        label?.textColor = UIColor(rgb:0x58595B)
        let  label1 = cell.viewWithTag(3000+indexPath.row) as? UILabel
        if label1?.tag != nil {
            timeSelected = checkTimeSelected((label1?.tag)!)
            for eachTime in timeArray {
                if eachTime == timeSelected {
                    if let index = timeArray.index(of: eachTime) {
                        timeArray.remove(at: index)
                        timingsArry.remove(at: index)
                         dataList[index] = ""
                         arrayofTextField.remove(at: index)
                        medicineTimeTableView.reloadData()
                    }
                }
            }

回答1:


It's because you are using dequeueReusableCell which reuses the other cells.

in your AddMedicationTableViewCell I therefore suggest you use:

override func prepareForReuse() {
        super.prepareForReuse()
    }

To reset the cell to basic.




回答2:


You should have a array of string intialization at gloabl.

var textValues = [String]()

In your datasource cellForRowAtIndexPath

Set

textField.tag = indexPath.row
cell.textField.text = textValues[indexPath.row]

And TextField delegates in your viewcontroller.

func textFieldDidEndEditing(_ textField: UITextField) {
    if let aText = textField.text {
        textValues[textField.tag] = aText
    }
}


来源:https://stackoverflow.com/questions/49791041/scrollview-of-uitableview-with-multiple-textfield-shows-copy-value-from-one-to-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!