2017-05-31 8 views
0

次のようにカスタム表のセルがあります。画像(左)、ラベル(中)、別の画像(右)を表示しようとしています。私はxibを作成し、xibをこのカスタムテーブルのセルクラスに関連付けました。そして、私は識別子として "sensorStatusTableViewCell"を使用しています。 SensorStatusTableViewCellをカスタムクラスに追加しました。iOS swift 3 - カスタム表のセルですが、表ビューにデータが表示されません。

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])") 
} 
} 

これを実行すると、テーブルビューが表示されますが、カスタムテーブルセルは表示されません。空のテーブル行だけが表示されます。私がする必要があることは他にありますか?

ここは私のスクリーンショットです。

enter image description here

+0

これは、uitableviewとセルを作成する奇妙な方法です。 @IBOutletの使用は、scrollViewとtableviewのセルをレイアウトするxibファイルまたはストーリーボードがあることを示していますが、cellForRowat()関数ではセルのメンバーを上書きしています。 – onnoweb

+0

テーブルビューセルの高さを指定していないようです。 テーブルビューのデリゲートメソッドを実装する必要があります - 'func tableView(_ tableView:UITableView、heightForRowAt indexPath:IndexPath) - > CGFloat' – tek3

+0

この例は次のとおりです。 https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/custom-cells/ この例では、コードに高さがありません。 – user826323

答えて

0

TableViewCellの識別子が "sensorStatusTableViewCell" です。 StoryBoard - > SensorStatusTableViewCellを確認してください。識別子は "sensorStatusTableViewCell"でなければなりません。

関連する問題