我一直堅持這個問題好幾天,所以如果有人能幫忙,我會很開心。 我想創建一個動態的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
}
}
嘗試使用dequeueReusableCellWithIdentifier(「cell」,forIndexPath:indexPath)並確保您的單元格ID匹配,並且如果單元格不是可選的,則不必使用cell.labDesk?.text,只需使用cell.labDesk.text – Ian 2015-03-13 15:35:16
我已經試過了,它給了我這個異常: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