菜鸟教程小白 发表于 2022-12-11 20:34:26

ios - 无法将自定义单元格与 UITableView 一起使用


                                            <p><p>我正在尝试使用自定义填充 uitableview,但出现以下错误:</p>

<pre><code>Fatal error: Use of unimplemented initializer &#39;init(style:reuseIdentifier:)&#39; for class &#39;Appname.PostCellView&#39;
</code></pre>

<p>代码:</p>

<pre><code>import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: &#34;Cell&#34;) as! PostCellView
      return cell
    }

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
      super.viewDidLoad()

      tableView.register(PostCellView.self, forCellReuseIdentifier: &#34;Cell&#34;)
      // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
    }
</code></pre>

<p>Post View Cell:(这个类被加载到 View 的文件所有者中)</p>

<pre><code>import UIKit
protocol PostCellViewDelegate: class {

}

class PostCellView: UITableViewCell {
    weak var delegate:PostCellViewDelegate?


    required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)

      let _ = commonInitialization()

    }

    func customise(imageName : String , color : UIColor, logoLabelValue : String, websiteValue: String)
    {
    }

    func commonInitialization() -&gt; UIView
    {
      let bundle = Bundle.init(for: type(of: self))
      let nib = UINib(nibName: &#34;PostCellView&#34;, bundle: bundle)
      let view = nib.instantiate(withOwner: self, options: nil) as! UIView
      view.frame = bounds
      view.autoresizingMask =
      addSubview(view)
      return view

    }

}
</code></pre>

<p>请帮我找出我的代码有什么问题以及我应该如何纠正。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>覆盖 <code>init(style:reuseIdentifier:)</code></p>

<pre><code>override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    // Code
}
</code></pre>

<p>请查看 <a href="http://studyswift.blogspot.com/2015/09/uitableviewcell-create-custom-prototype.html" rel="noreferrer noopener nofollow">tutorial</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法将自定义单元格与 UITableView 一起使用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52289078/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52289078/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法将自定义单元格与 UITableView 一起使用