[英]Table view cells not showing in simulator
我似乎已經完成了我應該做的所有事情,但是當我使用兩個帶有xib文件的UITableViewCell的兩個不同子類時,我的表視圖單元格沒有顯示,除了最后分配給textLabel!.text的整數值tableView(_:cellForRowAt :)在return語句之前。 我究竟做錯了什么?
這是我的代碼:
import UIKit
class DetailTableViewController: UITableViewController {
let items = [0, 1]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(DueDateSwitchTableViewCell.self, forCellReuseIdentifier: "DueDateSwitchTableViewCell")
let xibDueDateSwitchTableViewCell = UINib(nibName: "DueDateSwitchTableViewCell", bundle: Bundle.main)
tableView.register(xibDueDateSwitchTableViewCell, forCellReuseIdentifier: "DueDateSwitchTableViewCell")
tableView.register(DueDatePickerTableViewCell.self, forCellReuseIdentifier: "DueDatePickerTableViewCell")
let xibDueDatePickerTableViewCell = UINib(nibName: "DueDatePickerTableViewCell", bundle: Bundle.main)
tableView.register(xibDueDatePickerTableViewCell, forCellReuseIdentifier: "DueDatePickerTableViewCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("tableView(_:cellForRowAt:)", "indexPath.row=", indexPath.row)
let cell = UITableViewCell()
switch indexPath.section {
case 0:
print("\tcase 0")
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDateSwitchTableViewCell", for: indexPath) as! DueDateSwitchTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.backgroundColor = UIColor.yellow
case 1:
print("\tcase 1")
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDatePickerTableViewCell", for: indexPath) as! DueDatePickerTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.datePicker.date = Date()
default:
break
}
cell.textLabel!.text = String(items[indexPath.section].hashValue)
return cell
}
}
您在每個case
語句中創建的單元格將被忽略,並保持未使用狀態。 您的return cell
行將返回第一個cell
變量,即您的空單元格。
由於您只有兩個可能的單元格,因此建議您按照以下方式重做代碼:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDateSwitchTableViewCell", for: indexPath) as! DueDateSwitchTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.backgroundColor = UIColor.yellow
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDatePickerTableViewCell", for: indexPath) as! DueDatePickerTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.datePicker.date = Date()
return cell
}
}
您還需要修復viewDidLoad
。 每個重用標識符應該只有一個調用來register
。 你們兩個。
更改ViewDidLoad
方法
let nib = UINib(nibName: "DueDateSwitchTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "DueDateSwitchTableViewCell")
let nib = UINib(nibName: "DueDatePickerTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "DueDatePickerTableViewCell")
並更改cellForRowAtIndexPath
dataSource方法
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("tableView(_:cellForRowAt:)", "indexPath.row=", indexPath.row)
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDateSwitchTableViewCell", for: indexPath) as! DueDateSwitchTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.backgroundColor = UIColor.yellow
cell.textLabel!.text = String(items[indexPath.section].hashValue)
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDatePickerTableViewCell", for: indexPath) as! DueDatePickerTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.datePicker.date = Date()
cell.textLabel!.text = String(items[indexPath.section].hashValue)
return cell
}
}
}
如果您使用的是xibs,則無需在register方法中注冊UITableViewCell子類,只需使用nib注冊
override func viewDidLoad() {
super.viewDidLoad()
let xibDueDateSwitchTableViewCell = UINib(nibName: "DueDateSwitchTableViewCell", bundle: Bundle.main)
tableView.register(xibDueDateSwitchTableViewCell, forCellReuseIdentifier: "DueDateSwitchTableViewCell")
let xibDueDatePickerTableViewCell = UINib(nibName: "DueDatePickerTableViewCell", bundle: Bundle.main)
tableView.register(xibDueDatePickerTableViewCell, forCellReuseIdentifier: "DueDatePickerTableViewCell")
}
在cellForRowAt方法中,您通過將單元格分配給UITableViewCell()返回空單元格,將其更改為返回您已基於indexPath.section在ViewDidLoad中注冊的CustomCells作為numberOfSection方法,返回2個部分
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDateSwitchTableViewCell", for: indexPath) as! DueDateSwitchTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.backgroundColor = UIColor.yellow
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "DueDatePickerTableViewCell", for: indexPath) as! DueDatePickerTableViewCell
cell.label.text = String(items[indexPath.section].hashValue)
cell.datePicker.date = Date()
return cell
default:
fatalError("Unexpected section \(indexPath.section)")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.