具有多个文本字段的 UiTableview 的滚动视图显示从一个到另一个的复制值 [英] Scrollview of UiTableview with multiple textfield shows copy value from one to another

查看:18
本文介绍了具有多个文本字段的 UiTableview 的滚动视图显示从一个到另一个的复制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临动态单元格的问题,它根据我的用药时间的选择创建多个文本字段.当我根据创建的文本字段输入选择用药时间时,如果我取消选择用药时间,文本字段应该从工作正常的 uiTableview 单元格中删除. 我在滚动 tableview 时遇到的问题,因为第一个文本字段值被复制到第 5 个文本字段,反之亦然.同样在删除文本文件时,值会被复制

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
    }

//删除代码

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

推荐答案

你应该在 globabl 有一个字符串初始化数组.

You should have a array of string intialization at gloabl.

var textValues = [String]()

在您的数据源 cellForRowAtIndexPath

设置

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

TextField 委托在您的视图控制器中.

And TextField delegates in your viewcontroller.

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

这篇关于具有多个文本字段的 UiTableview 的滚动视图显示从一个到另一个的复制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆