【问题标题】:implementing UITextFieldDelegate实现 UITextFieldDelegate
【发布时间】:2017-01-23 18:53:07
【问题描述】:

我想在我的项目中使用https://github.com/intuit/AnimatedFormFieldTableViewCell,但我无法理解设置它的步骤。到目前为止,我已经从文件夹中拖出文件并按照以下步骤操作:

要使用AnimatedFormFieldTableViewCell,您只需:

1) 在要实现 UITableView 的 ViewController 上注册 AnimatedFormFieldTableViewCell nib 以作为重用标识符。

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerCellNib(AnimatedFormFieldTableViewCell)

    tableView.reloadData()
}

2) 将 CellForRowAtIndexPath 上的单元格作为 AnimatedFormFieldTableViewCell 出列。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AnimatedFormFieldTableViewCell

    return cell
}

3) 通过对单元格本身调用 setLabelText 来更改占位符的标签文本。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AnimatedFormFieldTableViewCell

    cell.setLabelText("Enter title")

    return cell
}

注意事项:

1) 您仍然可以像在常规 UITextField 上实现相同的方式实现 UITextFieldDelegate,您只需要定义 AnimatedFormFieldTableViewCell 的委托(无需直接为嵌入式 UITextField 定义委托)。

2) 要访问嵌入的 UITextField,您只需调用单元格的 cellTextField 属性即可。

I don’t get these last two steps.

如果我运行我的应用程序,我会在 AnimatedFormFieldTableViewCell 类中的 self.cellTextfield.delegate = self 上获得 unexpectedly found nil

我错过了什么?

【问题讨论】:

  • 您在哪里访问cellTextField?您能否也包括该代码?文档明确指出there is no need to directly define the delegate for the embedded UITextField。您可能应该将 self.delegate 设置为您想要的委托,而不是 self.cellTextField.delegate
  • @MichaelFourre 我不明白。我没有访问cellTextField。我也没有实现UITextFieldDelegate。我不知道该怎么做。我遇到错误的self.cellTextField.delegate 是在AnimatedFormFieldTableViewCell 类中实现的,而不是在我的类中实现的。
  • @MichaelFourre 不访问cellTextField 可能是我收到意外发现的 nil 错误的原因。但我想按顺序按照步骤正确实施。
  • 好吧,我现在明白了。 @waseefakhtar 让我看看我是否能弄清楚发生了什么
  • @waseefakhtar,我在我的项目中测试了AnimatedFormFieldTableViewCell,没问题,那么,你的意思是什么?

标签: ios swift uitableview uitextfield


【解决方案1】:

您好waseefakhtar 您的代码不会遗漏任何内容。唯一的问题是您以错误的方式注册了笔尖,这就是您在self.cellTextfield.delegate = self 上收到unexpectedly found nil 错误的原因。

试试这个代码:

  let myNib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)
  self.tableView.registerNib(myNib, forCellReuseIdentifier: "cell")

PS:请注意 registerNib 方法的语法可能因 swift 版本而异

【讨论】:

    【解决方案2】:

    我的代码在这里,我不知道你笔误在哪里,但你可以查看我的代码找出:

    import UIKit
    
    class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // register a nib like below 
            let nib:UINib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)
    
            self.tableView.register(nib, forCellReuseIdentifier: "AnimatedFormFieldTableViewCell")
    
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3
        }
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
            return 88.0
       }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "AnimatedFormFieldTableViewCell", for: indexPath as IndexPath) as! AnimatedFormFieldTableViewCell
    
            cell.setLabelText("Enter title")
    
            return cell
        }
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            tableView.deselectRow(at: indexPath, animated: true)
        }
    
    }
    

    结果:

    所以,只要检查一下步骤,哪里出错了。

    【讨论】:

    • 谢谢。我知道我正在查看错误的错误声明,因此发布了我已实施的所有步骤。但是没有人指出我没有正确注册笔尖。
    【解决方案3】:

    文档说明了这一点:

    There is no need to directly define the delegate for the embedded UITextField

    指令是执行以下操作:

    You just need to define the AnimatedFormFieldTableViewCell's delegate

    您需要设置AnimatedFormFieldTableViewCell 的委托而不是cellTextField 的委托,所以:

    self.delegate = yourUITextViewDelegate

    而不是self.cellTextField.delegate = yourUITextViewDelegate(这是错误的)

    这是文档描述的内容

    编辑:OP 澄清委托分配代码不在他们的班级;它在 pod 的类中。我正在考虑删除这个答案,因为我认为它不能解决问题。

    【讨论】:

    • 我该如何设置AnimatedFormFieldTableViewCell的代理?在viewDidLoad()我想在哪里实现呢? yourUITextViewDelegate 是什么?
    • 您可以在您的UITableViewControllertableView(_:cellForRowAt:) 方法中设置delegate,通过分配cell.delegate = self。然后,您需要仔细检查您的 UITableViewController 是否采用 UITextViewDelegate 协议 @waseefakhtar
    • 到目前为止,我已经添加了UITextFieldDelegate 以及UITableViewDataSource 的扩展名并实现了cell.delegate = self。如果我不添加UITextFieldDelegate,我会收到错误:Cannot assign value of type ‘MyClass' to type 'UITextFieldDelegate?’
    • 是的,这是预期的
    • 我仍然会得到同样的unexpectedly found nil 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多