菜鸟教程小白 发表于 2022-12-11 18:24:02

ios - UITableView 委托(delegate)方法未在 swift 3 中调用


                                            <p><p>我有一个 TableView ,它是子类化和扩展的,然后在 ViewController 中设置。我遇到的问题是委托(delegate)方法 ViewForHeaderInSection 没有被调用,而普通数据源方法被调用。 </p>

<p>(这是TableView的设置方法,在ViewDidLoad中调用。表格 View 通过IBOutlet连接到View Controller)</p>

<pre><code>func setup() {
    self.dataSource = self as UITableViewDataSource
    self.delegate = self
    let nib = UINib(nibName: &#34;MyTableViewCell&#34;, bundle: nil)
    self.register(nib, forCellReuseIdentifier: &#34;MyCell&#34;)

}
</code></pre>

<p>这些是扩展</p>

<pre><code>extension MyTableView: UITableViewDataSource,UITableViewDelegate {


    func numberOfSections(in tableView: UITableView) -&gt; Int {
      //print(Genres.total.rawValue)
      return Genres.total.rawValue
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      if let tableSection = Genres(rawValue: section), let articleData = articleDictionary{
          // print(articleData.count)
            return articleData.count
      }
      print(0)
      return 0
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

         let cell = tableView.dequeueReusableCell(withIdentifier: &#34;MyCell&#34;) as! MyTableViewCell

      if let tableSection = Genres(rawValue: indexPath.section), let article = articleDictionary?{
            cell.cellTitle.text = article.articleTitle
            cell.cellImageView.image = article.articleImage
            cell.cellEmojiReaction.text = article.articleEmojis
      }


      return cell
    }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
      let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 40.0))
      view.backgroundColor = .brown
      let label = UILabel(frame: CGRect(x: 16, y: 0, width: tableView.bounds.width, height: 40))
      label.textColor = .blue

      let tableSection = Genres(rawValue: section)
      switch tableSection {
      case .breakingNews?:
            label.text = &#34;Breaking News&#34;
      case .scienceAndTech?:
            label.text = &#34;Science and Tech&#34;
      case .entertainment?:
            label.text = &#34;Entertainment&#34;
      case .sports?:
            label.text = &#34;Sports&#34;
      default:
            label.text = &#34;Invalid&#34;
      }
      print(&#34;Function Run&#34;)
      view.addSubview(label)
      print(label.text ?? &#34;Nothing Here&#34;)
      return view
    }

}
</code></pre>

<p>这是 ViewController 代码:</p>

<pre><code>class ViewController: UIViewController {


    @IBOutlet weak var myTableView: MyTableView!

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

}
</code></pre>

<p>委托(delegate)方法没有被调用有什么具体原因吗?提前感谢您的宝贵时间。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>为此,您还必须实现另外一种方法 <code>heightForHeaderInSection</code> 以及 <code>viewForHeaderInSection</code> 方法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView 委托(delegate)方法未在 swift 3 中调用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45407154/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45407154/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView 委托(delegate)方法未在 swift 3 中调用