2015-03-13 97 views
11

我一直堅持這個問題好幾天,所以如果有人能幫忙,我會很開心。 我想創建一個動態的UITableView,爲此我創建了一個自定義的UITableView子類,並且我也創建了一個自定義的UITableViewCell子類,因爲我需要在每個單元中有多個UILabels和一個UIButton。 單元格已創建,但問題是標籤的值始終爲零,因此單元格顯示不正確。 This is,故事板的樣子,以及this is在運行程序時看到的內容。Swift自定義UITableViewCell標籤始終爲零

這裏是我的UITableViewCell子類:

import UIKit 

class QuestionTableViewCell: UITableViewCell { 

    @IBOutlet var student: UILabel! 
    @IBOutlet var labDesk: UILabel! 
    @IBOutlet var topic: UILabel! 
    @IBOutlet var answers: UILabel! 

} 

和我的UITableView子類:

import UIKit 

class QuestionViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet var table: UITableView! 
    struct Question { 
     var student: String 
     var labDesk: String 
     var topic: String 
     var answered: String 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     table.estimatedRowHeight = 50 
     table.dataSource = self 
     table.delegate = self 
     self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell") 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as QuestionTableViewCell 
     cell.student.text = "random string" 
     cell.labDesk?.text = "25/A" 
     cell.topic?.text = "string" 
     cell.answers?.text = "3" 
     return cell 
    } 
} 
+1

嘗試使用dequeueReusableCellWithIdentifier(「cell」,forIndexPath:indexPath)並確保您的單元格ID匹配,並且如果單元格不是可選的,則不必使用cell.labDesk?.text,只需使用cell.labDesk.text – Ian 2015-03-13 15:35:16

+0

我已經試過了,它給了我這個異常:2015-03-13 15:31:09.626 LABhelper [40046:5574601] ***在 - [UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/ SourceCache/UIKit_Sim/UIKit- 3318.16.14/UITableView的。m:6116這就是我的函數看起來像什麼後,你建議如果我得到它的權利︰var cell = tableView.dequeueReusableCellWithIdentifier(「cell」,forIndexPath:indexPath)QuestionTableViewCell 我沒有讓他們可選,但不知何故,他們成爲可選項,所以沒有?構建失敗。 – 2015-03-13 15:38:50

回答

4

首先,你沒有,如果它在Interface Builder存在註冊類。

其次,您應該dequeueReusableCellWithIdentifier:forIndexPath而不是dequeueReusableCellWithIdentifier

第三,UITableViewController已經有一個名爲tableView性質,所以沒有必要讓一個IBOutlet到table爲的UITableViewController已經處理了這一點。它也符合UITableViewDataSourceUITableViewDataSource,所以這些是無關的。

四,不要將table的屬性設置爲tableView

第五,cell.labDesk.text = ""就足夠了,不需要使它成爲可選項。

如果所有的IBOutlet都連接好了,單元標識符正確設置,並且這些修訂已完成,它將起作用。

class QuestionTableViewCell: UITableViewCell { 

    @IBOutlet var student: UILabel! 
    @IBOutlet var labDesk: UILabel! 
    @IBOutlet var topic: UILabel! 
    @IBOutlet var answers: UILabel! 

} 


class QuestionViewController: UITableViewController { 

    struct Question { 
     var student: String 
     var labDesk: String 
     var topic: String 
     var answered: String 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.estimatedRowHeight = 50 
     tableView.dataSource = self 
     tableView.delegate = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as QuestionTableViewCell 
     cell.student.text = "random string" 
     cell.labDesk.text = "25/A" 
     cell.topic.text = "string" 
     cell.answers.text = "3" 
     return cell 
    } 
} 
+0

我已經告訴過你了,如果它不是可選的,它不會運行。 cell.student.text =「隨機字符串」 致命錯誤:在解包可選值時意外發現nil – 2015-03-13 16:09:55

+0

它會在編譯時崩潰嗎? – Ian 2015-03-13 16:11:40

+0

它一旦我到達UITableView – 2015-03-13 16:12:26

0

我可能會遲到,但我剛剛解決了類似的問題。

確保你已經在你的UITableViewCell上設置了InterfaceBuilder中的標識符。

enter image description here

28

嘗試刪除self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")

+7

我可以吻你❤️ – daidai 2017-02-07 06:12:09

+0

它的工作正常。但爲什麼我們要刪除這條線bcz註冊customcell儀式? – iApple 2017-02-20 13:21:53

+0

同意@daidai謝謝 – iBug 2017-07-25 12:32:26

12

如果您使用的是電池用筆尖則請確保您註冊使用registerNib:forCellReuseIdentifier:表視圖中的單元格。如果單元格只有一個類,則使用registerClass:forCellReuseIdentifier:

+3

如果您使用的是筆尖,您需要的確切語法是:'tableView.registerNib(UINib(nibName:「YOURNIBNAME」,bundle:nil),forCellReuseIdentifier:「YOURREUSEID」)' – buczek 2016-04-28 18:02:08

0

對於那些誰仍然在努力嘗試所有可能的解決方案後,這出:

斷開/重新連接在你的故事板的IBOutlets應該做的伎倆!