2017-08-13 26 views
0

我在將表格視圖單元格轉換爲dequeueReusableCell(withIdentifier: for:)步驟中的自定義表格視圖單元格時遇到問題。無法使用來自xib文件的dequeueReusableCell(withIdentifier:for :)

我的代碼:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell 
    cell.labelTitle.text = array[indexPath.row] 
    return cell 
} 

我已經註冊了viewDidLoad()我的手機:

tableView.register(TextFieldTableViewCell.self, forCellReuseIdentifier: "TextFieldcell") 

而且我得到了一個錯誤說:

fatal error: unexpectedly found nil while unwrapping an Optional value

我打賭失敗鑄造,這就是爲什麼細胞是空的。任何人都可以看到錯誤?

謝謝!

更新的代碼

override func viewDidLoad() { 
     super.viewDidLoad() 
     let bundle = Bundle(for: TextFieldTableViewCell.self) 
     let nib = UINib(nibName: "MyTableViewCell", bundle: bundle) 
     tableView.register(nib, forCellReuseIdentifier: "TextFieldcell") 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return array.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell 
     cell.labelTitle.text = array[indexPath.row] 
     return cell 
    } 
} 

因此很明顯,問題仍然存在,澄清,這裏是我創建這個自定義的UITableViewCell

  1. 創建.swift文件和步驟。 xib文件
  2. 在.xib文件中,我刪除了樣板代碼並添加了兩個UILabels
  3. 我將.xib文件的文件所有者設置爲我的swift,其中包含我的兩個IBOutlets
  4. 接下來,在我的tableViewController中,我想實例化我自定義的tableViewCell,在viewDidLoad()方法中,我添加了以下內容:

let bundle = Bundle(for: TextFieldTableViewCell.self)

let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)

tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")

我創建此代碼推理情況如下:■因爲我使用.xib文件和接口生成器創建了我的自定義表格視圖單元格,我需要使用tableView.register(nibName: forCellReuseIdentifier:)函數將xib加載到我的tableViewController(糾正我,如果我錯了)。

然而,當我運行代碼,我得到了一個錯誤說:reason: 'Could not load NIB in bundle: 'NSBundle

這裏是我的我的自定義單元代碼:

class TextFieldTableViewCell: UITableViewCell { 

    @IBOutlet weak var labelTitle: UILabel! 
    @IBOutlet weak var userInput: UITextField! 
} 

任何人都能看出問題出在哪裏?謝謝!

回答

1

如果您UITableViewCell子類是設計在Interface Builder中你應該使用

func register(_ nib: UINib?, forCellReuseIdentifier identifier: String) 

方法。例如:

let nib = UINib(nibName: "\(TextFieldTableViewCell.self)", bundle: nil) 
tableView.register(nib, forCellReuseIdentifier: "TextFieldcell") 

如果在代碼中完全創建了UITableViewCell子類,你應該使用

func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String) 

方法。

如果您在故事板中使用原型單元格,則根本不應註冊單元格。

+0

感謝您的答覆,我已經創建了我的手機在廈門國際銀行文件,我已經註冊了我的手機,但不知何故,失敗 –

+0

在你的問題你說你正在使用' func寄存器(_ cellClass:Swift.AnyClass ?, forCellReuseIdentifier標識符:字符串)'。但是如果你使用了一個xib,你應該使用'func register(_nib:UINib?,forCellReuseIdentifier identifier:String)'。如果情況並非如此,也許你可以更新你的問題,以更準確地反映你正在做的事情。 – beyowulf

+0

謝謝修復它!感謝您的幫助! –

1

您需要註冊您的筆尖對象。

在viewDidLoad中():

let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil) 

tableView.register(nib, forCellReuseIdentifier: "yourIdentifier") 
相關問題