[英]iOS swift 3 - custom table cell, but no data is showing on the tableview
我有一個自定義表格單元,如下所示。 我正在嘗試顯示一個圖像(左),標簽(中間),另一個圖像(右)。 我創建了xib,並將xib與該自定義表格單元格類相關聯,並且我使用“ sensorStatusTableViewCell”作為標識符。 我也將SensorStatusTableViewCell添加到自定義類。
class SensorStatusTableViewCell: UITableViewCell {
@IBOutlet weak var sensorImage: UIImageView!
@IBOutlet weak var sensorNameLabel: UILabel!
@IBOutlet weak var sensorOnOffStatusLabel: UIImageView!
}
class ViewController: SuperViewController, UITableViewDelegate, UITableViewDataSource {
var sensors: [String] = ["Door Sensor"]
@IBOutlet var scrollView: UIScrollView!
var sensorTableView: UITableView = UITableView()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
sensors = ["Door Sensor"]
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = CGFloat(200)
sensorTableView.frame = CGRect(x: 0, y: 200, width: screenWidth, height: screenHeight);
sensorTableView.dataSource = self
sensorTableView.delegate = self
sensorTableView.register(SensorStatusTableViewCell.self, forCellReuseIdentifier: "sensorStatusTableViewCell")
self.scrollView.addSubview(sensorTableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:SensorStatusTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sensorStatusTableViewCell", for: indexPath) as! SensorStatusTableViewCell
let sensorImageName = "sensor_door.png"
let sensorImage = UIImage(named: sensorImageName)
let sensorImageView = UIImageView(image: sensorImage!)
cell.sensorImage = sensorImageView
let sensorLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
sensorLabel.center = CGPoint(x: 160, y: 285)
sensorLabel.text = sensors[0]
cell.sensorNameLabel = sensorLabel
let sensorStatusName = "icon_sensor_on.png"
let sensorStatusImage = UIImage(named: sensorStatusName)
let sensorStatusImageView = UIImageView(image: sensorStatusImage!)
cell.sensorOnOffStatusLabel = sensorStatusImageView
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
print("User selected table row \(indexPath.row) and item \(sensors[indexPath.row])")
}
}
運行此命令時,我看到一個表格視圖,但是沒有看到自定義表格單元格。 我只看到空表行。 還有什么我需要做的嗎?
這是我的截圖。
TableViewCell的標識符為“ sensorStatusTableViewCell”。 檢查您的StoryBoard-> SensorStatusTableViewCell。 它的標識符必須是“ sensorStatusTableViewCell”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.