如何在UITableViewCell内创建TextField?

时间:2018-08-22 01:56:45

标签: ios swift xcode debugging

我试图仅通过使用代码在 UITableViewCell 内创建 UITextField ,但此刻我将其设置为“ cell.mainTextField.delegate = self”,在“ cellForRowAt”内部,文本字段停止允许发短信,就像已被禁用一样。另外,功能 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

    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我认为问题是您正在将textField委托分配给控制器,因为它在TableViewCell中

  1. 在tableViewCell中编写textField委托方法
  2. 在tableView方法中,CellForRowAt赋予textField委托类似

cell.mainTextField.delegate = cell

或在TableViewCell文件中

var mainTextField: UITextField = {

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

        myTextField.placeholder = "Example"
        myTextField.delegate = self
        return myTextField
    }()

希望这会有所帮助