2017-01-23 127 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版本