我试图通过使用代码在UITableViewCell内创建UITextField,但此刻,我在“ cellForRowAt”内创建“ cell.mainTextField.delegate = self”时,该文本字段停止允许发短信,就像被禁用一样。同样,该函数shouldChangeCharactersIn()不在运行。

关于我在做什么错的任何想法吗?谢谢!

var publishing_TableView: UITableView?


类发布:

class Publishing: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        publishing_TableView = {

            let table = UITableView(frame: CGRect(x: 0, y: self.navigationController!.navigationBar.bounds.height, width: view.frame.width, height: view.frame.height), style: .grouped)

            table.backgroundColor = UIColor(white: 1, alpha: 1)
            table.delegate = self
            table.dataSource = self
            table.register(Publishing_MainTextField.self, forCellReuseIdentifier: "cell")
            table.separatorStyle = .none
            table.isScrollEnabled = true

            return table

        }

        view.addSubview(publishing_TableView!)

    }

}


表格视图功能:

extension Publishing: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let viewForHeader: UIView = UIView.init(frame: CGRect(x:0, y:0, width: view.frame.width, height: 0));

        viewForHeader.backgroundColor = .white

        return viewForHeader

    }


    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 0

    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        switch indexPath.row {

        case 0: return 60

        default: return 0

        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1

    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print("selected line: ", indexPath.row)

        tableView.deselectRow(at: indexPath, animated: false)

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1

    }

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

        switch indexPath.row {

        case 0:

            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Publishing_MainTextField

            cell.mainTextField.delegate = self

            cell.selectionStyle = .none

            return cell

        default: return UITableViewCell()

        }
    }
}


表格视图单元格功能:

class StartPublishing_MainTextField: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupViews()

    }

    required init? (coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    func setupViews() {

        addSubview(mainTextField)

    }

    var mainTextField: UITextField = {

        var myTextField = UITextField(frame: CGRect(x: 5, y: 5, width: UIScreen.main.bounds.width-2*20, height: 50))

        myTextField.placeholder = "Example"

        return myTextField
    }()
}


TextField委托功能:

extension Publishing: UITextFieldDelegate {

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

        var textFieldText: NSString = (textField.text ?? "") as NSString

        var  textAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)

        print("The updated text is: ", textAfterUpdate)

        return textAfterUpdate.characters.count <= 30

    }
}

最佳答案

import UIKit

class Publishing: UIViewController {

    var publishing_TableView : UITableView  {
        get {
            let table = UITableView(frame: CGRect(x: 0, y: self.navigationController!.navigationBar.bounds.height, width: view.frame.width, height: view.frame.height), style: .grouped)

            table.backgroundColor = UIColor(white: 1, alpha: 1)
            table.delegate = self
            table.dataSource = self
            table.register(StartPublishing_MainTextField.self, forCellReuseIdentifier: "cell")
            table.separatorStyle = .none
            table.isScrollEnabled = true
            return table
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.addSubview(publishing_TableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
extension Publishing: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let viewForHeader: UIView = UIView.init(frame: CGRect(x:0, y:0, width: view.frame.width, height: 0));
        viewForHeader.backgroundColor = .white
        return viewForHeader
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.row {
        case 0: return 60
        default: return 0
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("selected line: ", indexPath.row)
        tableView.deselectRow(at: indexPath, animated: false)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StartPublishing_MainTextField
            cell.mainTextField.delegate = self
            cell.selectionStyle = .none
            return cell
        default: return UITableViewCell()
        }
    }
}
class StartPublishing_MainTextField: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init? (coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews() {
        addSubview(mainTextField)
    }

    var mainTextField: UITextField = {
        var myTextField = UITextField(frame: CGRect(x: 5, y: 5, width: UIScreen.main.bounds.width-10, height: 50))
        myTextField.backgroundColor = #colorLiteral(red: 0.2181161642, green: 0.6374218464, blue: 1, alpha: 1)
        myTextField.layer.cornerRadius = 4.0
        myTextField.layer.borderWidth = 1.0
        myTextField.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
        myTextField.placeholder = "Type here"
        return myTextField
    }()
}
extension Publishing: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        var textFieldText: NSString = (textField.text ?? "") as NSString
        var  textAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
        print("The updated text is: ", textAfterUpdate)
        return textAfterUpdate.characters.count <= 30
    }
}


output

关于ios - 如何在UITableViewCell内创建TextField?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51958890/

10-12 02:06