我想在我的项目中使用https://github.com/intuit/AnimatedFormFieldTableViewCell,但是我搞不懂设置它的步骤。到目前为止,我已经从文件夹中拖出了文件,并按照如下步骤操作:
为了使用AnimatedFormFieldTableViewCell,你需要做的就是:
1)在ViewController上注册AnimatedFormFieldTableViewCell nib,在该ViewController上实现UITableView以实现您的重用标识符。
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerCellNib(AnimatedFormFieldTableViewCell)
tableView.reloadData()
}2)将您的计算单元作为AnimatedFormFieldTableViewCell在CellForRowAtIndexPath上出列。
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。
我遗漏了什么?
发布于 2017-01-25 07:44:47
我的代码在这里,我不知道你的文书错误在哪里,但你可以检查我的代码来找出:
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)
}
}结果是:

所以,只要检查一下步骤,你就错了。
发布于 2017-01-25 13:40:49
嗨,waseefakhtar,你的代码不会遗漏任何东西。唯一的问题是你用错误的方式注册了nib,这就是为什么你在self.cellTextfield.delegate = self上得到了unexpectedly found nil错误。
尝试以下代码:
let myNib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)
self.tableView.registerNib(myNib, forCellReuseIdentifier: "cell")PS:请注意,registerNib方法的语法可能会因swift版本而有所不同
发布于 2017-01-24 03:23:28
文档记录了这一点:
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的类中。我正在考虑删除这个答案,因为我认为它不能解决问题。
https://stackoverflow.com/questions/41813576
复制相似问题