SwiftのnibからカスタムUITableViewCellを作成する 質問する

SwiftのnibからカスタムUITableViewCellを作成する 質問する

私はペン先からカスタムテーブルビューセルを作成しようとしています。この記事を参考にしていますここ私は2つの問題に直面しています。

UITableViewCellオブジェクトをドラッグした.xibファイルを作成しました。サブクラスを作成しUITableViewCell、それをセルのクラスとして設定し、細胞再利用可能な識別子として。

import UIKit

class CustomOneCell: UITableViewCell {

    @IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!

    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

UITableViewControllerにはこのコードがあります。

import UIKit

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    var items = ["Item 1", "Item2", "Item3", "Item4"]

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

    // MARK: - UITableViewDataSource
    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let identifier = "Cell"
        var cell: CustomOneCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell
        if cell == nil {
            tableView.registerNib(UINib(nibName: "CustomCellOne", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell
        }

        return cell
    }
}

このコードはエラーなしでコンパイルされますが、シミュレーターで実行すると次のようになります。

ここに画像の説明を入力してください

ストーリーボードのUITableViewControllerではセルに何もしていません。識別子は空でサブクラスもありません。細胞プロトタイプ セルに識別子を追加して再度実行しましたが、同じ結果になりました。

私が直面したもう 1 つのエラーは、UITableViewController で次のメソッドを実装しようとしたときです。

override func tableView(tableView: UITableView!, willDisplayCell cell: CustomOneCell!, forRowAtIndexPath indexPath: NSIndexPath!) {

    cell.middleLabel.text = items[indexPath.row]
    cell.leftLabel.text = items[indexPath.row]
    cell.rightLabel.text = items[indexPath.row]
}

記事で述べたように、cellパラメータの型をUITableViewCellのサブクラスUITableViewCellに変更しました。しかし、次のエラーが発生します。CustomOneCell

セレクタ 'tableView:willDisplayCell:forRowAtIndexPath:' を使用したオーバーライド メソッドに互換性のない型 '(UITableView!, CustomOneCell!, NSIndexPath!) -> ()' があります

これらのエラーを解決する方法をご存知の方はいらっしゃいますか? これらは Objective-C では問題なく動作するようです。

ありがとう。

編集: シミュレータの向きを横向きにして縦向きに戻すと、セルが表示されることに気付きました。まだ何が起こっているのかわかりません。Xcodeプロジェクトをアップロードしました。ここ時間があれば、問題をすぐに確認してみましょう。

ベストアンサー1

Swift 5 と iOS 12.2 では、問題を解決するために次のコードを試してください。

カスタムセル.swift

import UIKit

class CustomCell: UITableViewCell {

    // Link those IBOutlets with the UILabels in your .XIB file
    @IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!

}

テーブルビューコントローラ.swift

import UIKit

class TableViewController: UITableViewController {

    let items = ["Item 1", "Item2", "Item3", "Item4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
    }

    // MARK: - UITableViewDataSource

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell

        cell.middleLabel.text = items[indexPath.row]
        cell.leftLabel.text = items[indexPath.row]
        cell.rightLabel.text = items[indexPath.row]

        return cell
    }

}

以下の画像は、Xcode からの制約の曖昧さのメッセージなしで、提供されたコードで機能する一連の制約を示しています。

ここに画像の説明を入力してください

おすすめ記事