ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController
<p><p>我正在尝试创建一个具有如下泛型类型的 VC:</p>
<pre><code>class SearchViewController<T>: UIViewControlle {
@IBOutlet weak var tableView: UITableView!
var delegate: SearchViewControllerDelegate?
fileprivate var dataArray: = []
...
}
extension SearchViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return showAddRow ? 1 : dataArray.count
}
}
</code></pre>
<p>但我收到此错误:</p>
<blockquote>
<p>Non-'@objc' method 'tableView(_:numberOfRowsInSection:)' does not
satisfy requirement of '@objc' protocol 'UITableViewDataSource'</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) -> Int
{
return 1
}
private func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 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]