Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

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

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()
                    }
                }
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...