繁体   English   中英

在Swift 4中,UITextField不会在UITableViewCell中结束编辑

[英]UITextField doesn't end editing in a UITableViewCell in Swift 4

我还是个新手,请教一下。 预先感谢您,我的英语不好。

我的目标是:

用户点击表格行中的编辑按钮。 出现UITextField而不是单元格。 输入值并按Return键后, UITextField再次消失,并重新计算单元格。

按下editButton >隐藏priceCell &显示UITextField &显示键盘并开始编辑/输入值(闪烁的光标)->通过按Return键停止编辑/输入值执行->隐藏UITextField &显示priceCell &将输入的值保存到数组中并重新加载已编辑行

我用这个答案作为开始的蓝图。

我也想使用.decimalPad键盘来更容易地输入数字值,并限制用户仅使用数字(和小数点),但这不包括将Return键用作停止编辑,对吗?

我找到可能的解决方案,但对我来说,这似乎很复杂...

我的ViewController:

import UIKit

class PortfolioViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, PortfolioCellDelegate {    

    let getData = GetData()

    ...

    override func viewDidLoad() {
        super.viewDidLoad()

        cellTableView.delegate = self
        cellTableView.dataSource = self

        cellTableView.register(UINib(nibName: "PortfolioCell", bundle: nil), forCellReuseIdentifier: "portfolioCell")

        self.currencyControl.selectedSegmentIndex = MyVariables.currencyControlSelected

    }

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

        let coinCell = tableView.dequeueReusableCell(withIdentifier: "portfolioCell", for: indexPath) as! PortfolioCell

        ...

        coinCell.delegate = self

        return coinCell
    }

    ...


    func portfolioButtonPressed(coinCell: PortfolioCell) {
        let indexPath = self.cellTableView.indexPathForRow(at: coinCell.center)!
        let selectedCell = cellTableView.cellForRow(at: indexPath) as! PortfolioCell

        selectedCell.priceCell.isHidden = true
        selectedCell.textCell.isHidden = false

        selectedCell.textCell.delegate = self

        func textFieldDidEndEditing(textField: UITextField) {
            let owned: Double = Double(textField.text!)!

            if owned >= 0 {
                MyVariables.dataArray[indexPath.row].ownedCell = owned
            } else {
                MyVariables.dataArray[indexPath.row].ownedCell = 0.00
            }

            selectedCell.priceCell.isHidden = false
            selectedCell.textCell.isHidden = true

            self.cellTableView.reloadData()
        }

        func textFieldShouldReturn(textField: UITextField) -> Bool {
            selectedCell.textCell.resignFirstResponder()
            return true
        }
    }
    ...
}

我的自定义单元格:

import UIKit

protocol PortfolioCellDelegate {
    func portfolioButtonPressed(coinCell: PortfolioCell)
}

class PortfolioCell: UITableViewCell {

    var delegate: PortfolioCellDelegate?

    ...

    @IBAction func editCellPressed(_ sender: UIButton) {
     delegate?.portfolioButtonPressed(coinCell: self)
    }
    ...
}

在此处输入图片说明

现在,当按下按钮时,会显示正确的UITextField ,但在按下Return键后不要关闭。

还是应该完全更改它并使用点击手势?

要结束任何一种scrollView的编辑,只需使用此

cellTableView.keyboardDismissMode = .onDrag

要么

cellTableView.keyboardDismissMode = .interactive

与tableView交互时,它将隐藏键盘

对于数字小键盘,您可以将工具栏添加为textField的inputAccessoryView。 在工具栏上,添加取消按钮以关闭键盘。

有两种方法:

1.)委托2.)IQKeyboardManager

1.)使用UITextFieldDelegate

有一个名为“ textFieldShouldEndEditing”的特定回调。在此方法中,返回true。

2.)使用IQKeyboardManager一个衬垫库。 该库自动管理所有TextField和scrollview。 您可以在AppDelegate中用一行激活它,因此易于使用。

https://github.com/hackiftekhar/IQKeyboardManager

工作,但不像想要的那样光滑,而且我也无法使IQKeyboardManager工作,所以我确实使用了inputAccessoryView

自定义单元:

import UIKit

protocol PortfolioCellDelegate {
    func portfolioButtonPressed(didSelect coinCell: PortfolioCell)
    func portfolioButtonPressed(coinCell:PortfolioCell, editingChangedInTextCell newValue:String)
}

class PortfolioCell: UITableViewCell, UITextFieldDelegate  {

    var delegate: PortfolioCellDelegate?

    ...

    @IBAction func editCellPressed(_ sender: UIButton) {
        textCell.becomeFirstResponder()
        delegate?.portfolioButtonPressed(didSelect: self)
    }

    @IBAction func textCellValueChanged(_ sender: UITextField) {

        if (sender.text?.isEmpty)! {
            delegate?.portfolioButtonPressed(coinCell: self, editingChangedInTextCell: "XXX")
        } else {
            let text = sender.text
            delegate?.portfolioButtonPressed(coinCell: self, editingChangedInTextCell: text!)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        self.textCell.delegate = self

        let flexiableSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneButtonAction))
        let toolBar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 35))

        toolBar.barTintColor = UIColor(red:0.15, green:0.69, blue:0.75, alpha:1.0)
        toolBar.tintColor = UIColor(red:0.93, green:0.93, blue:0.93, alpha:1.0)
        toolBar.setItems([flexiableSpace, doneButton], animated: false)

        textCell.inputAccessoryView = toolBar
        textCell.keyboardType = UIKeyboardType.decimalPad
    }

    @objc func doneButtonAction() {
        textCell.endEditing(true)
    }

    ...       
}

ViewController:

import UIKit

class PortfolioViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PortfolioCellDelegate {

    let getData = GetData()

    ...


    override func viewDidLoad() {
        super.viewDidLoad()        

        cellTableView.delegate = self
        cellTableView.dataSource = self

        cellTableView.register(UINib(nibName: "PortfolioCell", bundle: nil), forCellReuseIdentifier: "portfolioCell")

        self.currencyControl.selectedSegmentIndex = MyVariables.currencyControlSelected

        getData.delegate = self

        timeStampLabel.text = MyVariables.timeStamp
    }   

    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.cellTableView.reloadData()
        self.currencyControl.selectedSegmentIndex = MyVariables.currencyControlSelected
        self.timeStampLabel.text = MyVariables.timeStamp
    }

    //MARK: - tableView
    /***************************************************************/

    ...


    func portfolioButtonPressed(coinCell: PortfolioCell, editingChangedInTextCell newValue: String) {
        let indexPath = self.cellTableView.indexPathForRow(at: coinCell.center)!
        let selectedCell = cellTableView.cellForRow(at: indexPath) as! PortfolioCell

        selectedCell.priceCell.isHidden = false
        selectedCell.textCell.isHidden = true

        if newValue != "XXX" {
            let owned: Double = Double(newValue)!
            MyVariables.dataArray[indexPath.row].ownedCell = owned
        }

        selectedCell.priceCell.isHidden = false
        selectedCell.textCell.isHidden = true

        self.cellTableView.reloadRows(at: [indexPath], with: .automatic)
    }

    func portfolioButtonPressed(didSelect coinCell: PortfolioCell) {
        let indexPath = self.cellTableView.indexPathForRow(at: coinCell.center)!
        let selectedCell = cellTableView.cellForRow(at: indexPath) as! PortfolioCell

        selectedCell.priceCell.isHidden = true
        selectedCell.textCell.isHidden = false
    }
        ...

}

这很容易:您应该选择该表格视图单元,然后在属性检查器中启用“启用用户交互”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM