菜鸟教程小白 发表于 2022-12-11 18:49:22

ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController


                                            <p><p>我正在尝试创建一个具有如下泛型类型的 VC:</p>

<pre><code>class SearchViewController&lt;T&gt;: UIViewControlle {
    @IBOutlet weak var tableView: UITableView!
    var delegate: SearchViewControllerDelegate?
    fileprivate var dataArray: = []

...
}

extension SearchViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return showAddRow ? 1 : dataArray.count

    }
}
</code></pre>

<p>但我收到此错误:</p>

<blockquote>
<p>Non-&#39;@objc&#39; method &#39;tableView(_:numberOfRowsInSection:)&#39; does not
satisfy requirement of &#39;@objc&#39; protocol &#39;UITableViewDataSource&#39;</p>
</blockquote>

<p>我尝试过这样的 <code>@objc 方法</code>:</p>

<blockquote>
<pre><code>@objc(tableView:numberOfRowsInSection:)
</code></pre>
</blockquote>

<p>但它没有奏效。我错过了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要为 tableView 声明所有协议(protocol) ..</p>

<pre><code> func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -&gt; Int
{
    return 1
}
private func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int
{
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
{

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41998128/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41998128/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController