表视图单元格与文本字段

时间:2016-10-06 16:16:04

标签: ios uitableview delegates textfield

我有一个子类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!
}

此代码没有运行,我不确定我需要运行哪些文本字段代表才能从文本字段中获取Text值。

3 个答案:

答案 0 :(得分: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;
}

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

返回true将允许文本字段结束编辑。只有在用户想要停止编辑时才会调用此方法。因此,您应该从entranceFeeAmount = cell.entranceFeeTextField.text!方法移除cellForRowAtIndexPath,因为这是您创建单元格的位置。此时,用户不会输入您的文本字段,因此在创建文本后不会立即从中获取文本。

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

答案 1 :(得分:1)

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 .

答案 2 :(得分:0)

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

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
}

}