[英]implementing UITextFieldDelegate
我想在我的項目中使用https://github.com/intuit/AnimatedFormFieldTableViewCell ,但是我無法理解設置它的步驟。 到目前為止,我已經從文件夾中拖動了文件,並按照以下步驟操作:
為了使用AnimatedFormFieldTableViewCell
,您需要做的是:
1)在要為其實現UITableView的重用標識符的ViewController上注冊AnimatedFormFieldTableViewCell
筆尖。
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerCellNib(AnimatedFormFieldTableViewCell)
tableView.reloadData()
}
2)將您的單元格作為AnimatedFormFieldTableViewViewCell出隊在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
。
我想念什么?
嗨waseefakhtar您的代碼不會丟失任何東西。 唯一的問題是您以錯誤的方式注冊了筆尖,這就是為什么在self.cellTextfield.delegate = self
上unexpectedly found nil
錯誤的原因。
試試這個代碼:
let myNib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)
self.tableView.registerNib(myNib, forCellReuseIdentifier: "cell")
PS:請注意,registerNib方法的語法可能會有所不同,具體取決於快速版本
我的代碼在這里,我不知道您的筆誤在哪里,但是您可以檢查我的代碼以找出:
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)
}
}
結果:
因此,只需檢查錯誤的步驟即可。
該文檔指出:
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闡明了委托分配代碼不在其類中; 在豆莢的課上。 我正在考慮刪除此答案,因為我認為它不能解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.