菜鸟教程小白 发表于 2022-12-11 17:31:18

ios - swift 3 : Understand syntax change for UITableViewDataSource method


                                            <p><p>我对 Swift 3 函数调用有一些疑问。下面是一个例子。</p>

<p>老 swift :</p>

<pre><code>func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -&gt; UITableViewCell
</code></pre>

<p> swift3:</p>

<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
</code></pre>

<p>上面的语法很好。但现在 Xcode 向我显示一个错误并要求我执行以下操作:</p>

<pre><code>@objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
</code></pre>

<p>我不明白为什么我必须声明 <code>@objc(tableView:cellForRowAtIndexPath:)</code>.</p>

<p>仅当我尝试在扩展下实现 TableView 数据源方法时才会发生这种情况。 <code>numberOfRowsInSection</code> 或 <code>viewForHeaderInSection</code> 也不会发生这种情况。</p>

<p>谁能帮我理解为什么会这样?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>虽然我不确定是什么触发了 <code>@objc</code>,但我可以建议以下方法:</p>

<p>将 tableView 变量存储在 viewDidLoad 中的某处:</p>

<pre><code>let tv = tableView!
</code></pre>

<p>然后将鼠标悬停在 tableView 变量上并按下命令按钮并单击。</p>

<p><strong>这应该带您进入 UITableView 的界面</strong>。</p>

<p>然后,将鼠标悬停在 UITableViewDelegate 或 UITableViewDataSource 上,同时按下命令按钮并单击。</p>

<p>现在您可以看到新的签名。 </p>

<p>很多东西都变了...升级愉快!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios -swift3 : Understand syntax change for UITableViewDataSource method,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39519709/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39519709/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - swift 3 : Understand syntax change for UITableViewDataSource method