UITableView中的Swift UIStepper和UITextField文本重叠且无法同步

时间:2019-11-09 07:30:45

标签: ios swift uitextfield uistepper

我需要在单元格内有一个UITextField和一个UIStepper,并在两个值之一更改值时更新两者。我也使用tableviewcontroller达到了目标,但现在有两个问题:

1-要同步元素值,我创建了两个数组,用于维护每行的标签ID。为了确保所有标签都是唯一的,我从步进器的800开始,文本字段从900开始。问题是,当我在表内添加新行时,这些值总是重复的。例如,如果我添加一个新的行,而不是在数组内找到802,则会找到802和803,因此无法清楚地识别分配给每个步进器的文本。

2-当我通过步进更新值时,文本字段上的文本与上一个文本重叠,如下图所示:

enter image description here

仅当创建UITable时添加的值会发生这种情况,如果我最近添加一个值,则不会发生此问题。

我清楚地了解到,所有问题都与Swift出于性能原因所做的单元重用有关,但是我找不到解决方案。我的代码如下:

    class barcodeInfo: Codable {

    var code = ""
    var quantity = 1

    init(code: String, quantity : Int) {
        self.code = code
        self.quantity = quantity
    }
}


class BarcodeTableViewController: UITableViewController, UITextFieldDelegate {
    var barcodeList = [barcodeInfo]()
    var QuantityMapping = [Int: Int]()
    var currentStepperTag = 900
    var currentQtyTextTag = 800

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.rowHeight = 50;
        self.tableView.dataSource = self
        self.tableView.delegate = self
        barcodeList = [barcodeInfo(code:"test", quantity: 4),barcodeInfo(code:"test1", quantity: 10)]
    }


    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        var isBackSpace = Int32()

        if let char = string.cString(using: String.Encoding.utf8) {
            isBackSpace = strcmp(char, "\\b")
        }
        if (isBackSpace != -92) {
            if string.rangeOfCharacter(from: NSCharacterSet.decimalDigits) == nil {
                return false
            }
        }

        guard let textFieldText = textField.text,
            let rangeOfTextToReplace = Range(range, in: textFieldText) else {
                return false
        }
        let substringToReplace = textFieldText[rangeOfTextToReplace]
        let count = textFieldText.count - substringToReplace.count + string.count
        return count <= 5
    }



    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        print(barcodeList.count)
        return barcodeList.count
    }

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

        let currentBarcode = barcodeList[indexPath.row]
        print(currentBarcode)

        let cell = tableView.dequeueReusableCell(withIdentifier: "barcodeCell", for: indexPath) as UITableViewCell
        cell.selectionStyle = .none

        // cell.allowsSelection = false
        cell.textLabel?.text = currentBarcode.code

        let customStepper = UIStepper (frame:CGRect(x: self.view.frame.width - 225 , y: 5, width: 100, height: 20))
        //let customStepper = UIStepper()
        customStepper.autorepeat = true
        // Add a function handler to be called when UIStepper value changes
        customStepper.addTarget(self, action: #selector(stepperValueChanged(_:)), for: .valueChanged)
        customStepper.tag = currentStepperTag
        customStepper.maximumValue = 99999
        customStepper.minimumValue = 1
        customStepper.wraps = false
        customStepper.value = Double(currentBarcode.quantity)
        cell.contentView.addSubview(customStepper)

        customStepper.translatesAutoresizingMaskIntoConstraints = false
        customStepper.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
        customStepper.widthAnchor.constraint(equalToConstant: 94).isActive = true
        customStepper.heightAnchor.constraint(equalToConstant: 29).isActive = true
        customStepper.rightAnchor.constraint(equalTo: cell.rightAnchor, constant: -30).isActive = true

        let quantityTextField = UITextField(frame: CGRect(x: self.view.frame.width - 300, y: 5, width: 50, height: 20))

        //let quantityTextField = UITextField()
        quantityTextField.delegate = self
        quantityTextField.smartInsertDeleteType = UITextSmartInsertDeleteType.no
        quantityTextField.tag = currentQtyTextTag
        quantityTextField.text = String(Int(customStepper.value))
        cell.contentView.addSubview(quantityTextField)

        quantityTextField.translatesAutoresizingMaskIntoConstraints = false
        quantityTextField.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
        quantityTextField.widthAnchor.constraint(equalToConstant: 60).isActive = true
        quantityTextField.heightAnchor.constraint(equalToConstant: 21).isActive = true
        quantityTextField.rightAnchor.constraint(equalTo: cell.rightAnchor, constant: -125).isActive = true

        QuantityMapping[currentStepperTag] = currentQtyTextTag
        print("currentQtyTextTag is now \(Int(currentQtyTextTag))")
        print("currentStepperTag is now \(Int(currentStepperTag))")
        currentQtyTextTag += 1
        currentStepperTag += 1

        return cell
    }


    @IBAction private func stepperValueChanged(_ sender:UIStepper!){
        print(QuantityMapping)
        var tagDecrement = 800
        let textTag = QuantityMapping[sender.tag] ?? 0
        if let currentQuantityTextField = self.view.viewWithTag(textTag) as? UITextField {
            currentQuantityTextField.text = String(Int(sender.value))
            if(textTag >= 900){
                                 tagDecrement = 900
                             }
        //let itmIdx = ((textTag - tagDecrement)-2)
          let  itmIdx = 1
                  self.barcodeList[itmIdx].quantity = Int(sender.value)
        }
        print("sender tag: \(Int(sender.tag))")
        print("textTag \(Int(textTag))")
        print("UIStepper is now \(Int(sender.value))")
    }

1 个答案:

答案 0 :(得分:1)

因此可以为您的textview提供解决方案

cell.contentview.subviews.map(if $0.isKindOf(UITextView.self) { $0.removeFromSuperview() })

然后再执行addSubview(textView)

执行此操作,尽管为您的其他人添加了子视图-当然要进行正确的类型检查