首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在UITableViewCell中创建TextField?

如何在UITableViewCell中创建TextField?
EN

Stack Overflow用户
提问于 2018-08-22 09:56:45
回答 2查看 104关注 0票数 0

我正试图通过代码在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

    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-08-23 14:13:12

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

票数 0
EN

Stack Overflow用户

发布于 2018-08-23 15:16:51

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

在您的tableViewCell

  • In tableView method 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
    }()

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51958890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档