2017-01-23 126 views
0

我想在我的项目中使用https://github.com/intuit/AnimatedFormFieldTableViewCell,但我无法摆脱设置它的步骤。到目前为止,我已经拖从文件夹中的文件和遵循的步骤为:正在实施UITextFieldDelegate

为了使用AnimatedFormFieldTableViewCell,所有你需要做的是:

1)注册AnimatedFormFieldTableViewCell笔尖上的ViewController上你正在为你的重用标识符实现你的UITableView。

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)您仍可以实现UITextFieldDelegate你会定期的UITextField实现相同的方式,你只需要定义AnimatedFormFieldTableViewCell的代表(也没有必要直接定义委托嵌入的UITextField)。

2)为了访问嵌入的UITextField,你可以简单地调用单元格的cellTextField属性。

I don’t get these last two steps. 

如果我跑我的应用程序,我得到self.cellTextfield.delegate = selfunexpectedly found nilAnimatedFormFieldTableViewCell类。

我错过了什么?

+0

你在哪里访问'cellTextField'有什么不同?你能否也包括那些代码?该文档明确指出'不需要直接为嵌入的UITextField定义委托。你应该设置'self.delegate'到你想要的委托而不是'self.cellTextField.delegate'。 –

+0

@MichaelFourre我不明白。我没有访问'cellTextField'。我也没有实现'UITextFieldDelegate'。我不知道如何。我得到错误的'self.cellTextField.delegate'是在'AnimatedFormFieldTableViewCell'类中实现的,而不是在我的。 – waseefakhtar

+0

@MichaelFourre没有访问'cellTextField'可能是为什么我得到意外发现的零错误。但我希望按照这些步骤顺序执行。 – waseefakhtar

回答

1

我的代码是在这里,我不知道你在哪里写错了,但你可以检查我的代码,找出:

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) 
    } 

} 

结果:

enter image description here

所以,只需检查步骤,出错的地方。

+0

感谢您的支持。我知道我在看错误的错误陈述,因此公布了我已经实施的所有步骤。但没有人指出我没有正确注册笔尖。 – waseefakhtar

0

文档指出这一点:

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澄清,委托指派的代码是不是在他们的阶级;它在吊舱上课。我正在考虑删除这个答案,因为我不认为它正在解决问题。

+0

我该如何设置AnimatedFormFieldTableViewCell的委托?在'viewDidLoad()'我想实现它?什么是'yourUITextViewDelegate'? – waseefakhtar

+0

您可以设置在'UITableViewController'的'的tableView的'delegate'用'cell.delegate = self'分配'方法(_:cellForRowAt)。然后,你要仔细检查你的'UITableViewController'采用@waseefakhtar –

+0

到目前为止,我接着说:UITextFieldDelegate'与扩展'UITableViewDataSource'沿和实施'cell.delegate = self'了'UITextViewDelegate'协议。如果我不加'UITextFieldDelegate',我得到的错误:'不能分配的值类型“MyClass的”输入' – waseefakhtar

2

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版本