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

时间:2017-12-19 01:56:39

标签: ios swift uitableview uibutton uitextfield

我还是新手,我会向你征求意见。提前谢谢你,抱歉我的英语不好。

我的目标是:

用户点击表格行中的编辑按钮。 UITextField显示为单元格。输入值后,按ReturnUITextField会再次消失并重新计算单元格。

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

我使用this答案作为起始蓝图。

我还想使用.decimalPad键盘来更轻松地输入数值并限制用户仅使用数字(和小数点),但是这会排除使用Return键作为停止编辑,我是对的?

我找到了this可能的解决方案,但在我看来这个问题很复杂......

我的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)
    }
    ...
}

enter image description here

现在按下按钮时,UITextField显示正常,但在按下Return键后不要忽略。

或者我应该完全改变它并使用轻击手势?

5 个答案:

答案 0 :(得分:0)

要在任何类型的scrollView中结束编辑,只需使用此

即可
cellTableView.keyboardDismissMode = .onDrag

cellTableView.keyboardDismissMode = .interactive

与tableView交互时会隐藏键盘

答案 1 :(得分:0)

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

答案 2 :(得分:0)

还有两种方法:

1。)代表 2.)IQKeyboardManager

1)。 使用UITextFieldDelegate

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

2)。 使用IQKeyboardManager一个衬垫库。该库自动管理所有TextFields和scrollviews。你可以在AppDelegate中用一行激活它,这样它就很容易使用。

https://github.com/hackiftekhar/IQKeyboardManager

答案 3 :(得分:0)

工作但不像希望的那样光滑,而且我无法使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
    }
        ...

}

答案 4 :(得分:0)

这很容易:您应该选择该表格视图单元格,然后在属性检查器中启用 User Interaction Enabled