【问题标题】:Table View Cell with a Textfield带有文本字段的表格视图单元格
【发布时间】:2016-10-06 16:16:04
【问题描述】:

我有一个子类CustomCell,它继承自我的父类CreateEvent。该子类描述了表格视图单元格的各个单元格,它位于 CreateEvent 视图控制器上。在一个特定的单元格中,我有一个文本字段,它链接到 CustomCell 文件,但是当用户输入文本字段时,我无法从该文本字段中获取值。我也无法通过外部触摸关闭键盘并按返回键,但我主要专注于从文本字段中获取文本。我熟悉在普通的 swift 文件上执行这些功能,但因为这是一个子类,我不知道该怎么做。我尝试过的是使用:

class CustomCell: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var entranceFeeTextField: UITextField!

override func awakeFromNib() {
     super.awakeFromNib()

}

override func setSelected(selected: Bool, animated: Bool) {
     super.setSelected(selected, animated: animated)
}

还有:

class CreateEventVC: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate, UITextFieldDelegate {

override func viewDidLoad() {
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
    let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String, for: indexPath) as! CustomCell


    cell.entranceFeeTextField.delegate = self

    entranceFeeAmount = cell.entranceFeeTextField.text!
}

此代码未运行,我不确定需要运行哪些文本字段委托才能从文本字段中获取文本值。

【问题讨论】:

    标签: ios uitableview delegates textfield


    【解决方案1】:

    您可以使用UITextFieldDelegate 方法textFieldShouldEndEditing(:)textFieldShouldReturn(:) 来获取文本字段的结果。

    例如:

    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        print("TextField should end editing method called")
        let textFromCell = textField.text!
        //do whatever you want with the text!
        return true;
    }
    

    在这段代码 sn-p 中,textField 实际上是您的 entranceFeeTextField 实例。因为在某个地方,当该文本字段停止编辑时,它会调用self.delegate?.textFieldShouldEndEditing(entranceFeeTextField),并且该方法的实现在您的CreateEventVC 中。

    返回true 将允许文本字段结束编辑。仅当用户想要停止编辑时才会调用此方法。因此,您应该从您的 cellForRowAtIndexPath 方法中删除 entranceFeeAmount = cell.entranceFeeTextField.text!,因为这是您创建单元格的地方。那时,用户不会在您的文本字段中键入内容,因此一旦生成文本,就无法从其中获取文本。

    您所要做的就是在CreateEventVC 中实现其中一种方法。

    【讨论】:

    • 这很有帮助。我需要告诉文本字段停止编辑以便能够提取值是有道理的。但是,在“let textFromCell = textField.text!”行中,它不会识别来自 CustomCell 文件的 textField。
    • @Kevin 如果您正确设置了委托,并在您的CreateEventVC 中实现该方法,是的。该方法的参数为textField,但这只是委托传递的文本字段。当它被调用时,它将是你的entranceFeeTextField。除非你的意思是别的?
    • 我确实使用了 entryFeeTextField。一定是代表我太顺从了。不是'cell.entranceFeeTextField.delegate = self'吗?我还需要使用什么来满足代表?谢谢
    • 在方法中设置断点。看看它是否被调用过。尝试其他 UITextField 方法之一
    • 我刚刚收到错误消息“使用未解析的标识符 'entranceFieldTextField'”。我能够在 cellForRowAtIndexPath 中将文本字段委托设置为 self,但我不确定如何让 textFieldShouldEndEditing 函数与该委托进行通信以识别 entryFeeTextField
    【解决方案2】:
    Here is the full code: (Xcode 8 swift 3)
    (View Controller Class) 
    
    class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate
    {
        @IBOutlet weak var tbl: UITableView!
        var cell = TableViewCell()
        override func viewDidLoad()
        {
            super.viewDidLoad()
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        {
             cell = tbl.dequeueReusableCell(withIdentifier: "CELL") as! TableViewCell
            cell.configure(text: "", placeholder: "EnterText")
            return cell
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            return 1
        }
    
        func numberOfSections(in tableView: UITableView) -> Int
        {
            return 1
        }
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool
        {
    
    
           print( cell.returnTextOfTextField() )
            print(cell.txtField.text)
            cell.txtField .resignFirstResponder()
            return true
        }
    
    }
    
    
    TableViewCell class (Custom cell):
    class TableViewCell: UITableViewCell,UITextFieldDelegate
    {
    
        @IBOutlet weak var txtField: UITextField!
        override func awakeFromNib()
        {
            super.awakeFromNib()
            // Initialization code
        }
        public func configure(text: String?, placeholder: String) {
            txtField.text = text
            txtField.placeholder = placeholder
    
            txtField.accessibilityValue = text
            txtField.accessibilityLabel = placeholder
        }
    
        func returnTextOfTextField() -> String
        {
            print(txtField.text)
           return txtField.text!
        }
        override func setSelected(_ selected: Bool, animated: Bool)
        {
            super.setSelected(selected, animated: animated)
            // Configure the view for the selected state
        }
    
    }
    
    
    "CELL" is the identifier given to cell in Nib .
    

    【讨论】:

    • 当我运行我的代码时: cell = tableView.dequeueReusableCell(withIdentifier: "entranceFee") as! CustomCell,其中 entryFee 是我的包含文本字段的 .xib 文件,编译器抱怨“致命错误:在展开可选值时意外发现 nil”
    • 请参考链接你会发现代码:github.com/shemona-ios/stack
    【解决方案3】:

    这是工作代码,我从文本字段中获取值,甚至键盘也被辞职。

    var cell = TableViewCell() // customCell
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        {
             cell = tbl.dequeueReusableCell(withIdentifier: "CELL") as! TableViewCell
            cell.configure(text: "", placeholder: "EnterText")
            return cell
        }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 1
    }
    
    func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {
         //cell = tbl.dequeueReusableCell(withIdentifier: "CELL") as! TableViewCell
    
    
       print( cell.returnTextOfTextField() )
        print(cell.txtField.text)
        cell.txtField .resignFirstResponder()
        return true
    }
    
    
    
    
    /// Custom cell class
    
    
    class TableViewCell: UITableViewCell,UITextFieldDelegate
    {
    
    @IBOutlet weak var txtField: UITextField!
    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }
    public func configure(text: String?, placeholder: String) {
        txtField.text = text
        txtField.placeholder = placeholder
    
        txtField.accessibilityValue = text
        txtField.accessibilityLabel = placeholder
    }
    
    func returnTextOfTextField() -> String
    {
        print(txtField.text)
       return txtField.text!
    }
    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    

    }

    【讨论】:

    • 我在使用代码 'cell = tbl.dequeueReusableCell(withIdentifier: "CELL") 时遇到问题! TableViewCell cell.configure(text: "", placeholder: "EnterText") 返回单元格 }'。有很多错误弹出。我不确定 withIdentifier: "CELL" 和占位符: "EnterText" 是什么意思。
    • 请添加相同的快照。 TableViewCell 是自定义单元格的类。或者我能做的就是把代码发给你。添加您的邮件 ID 将发布代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多