2017-12-14 5 views
-2

が現在実践プロジェクトを作成し、私は本当に絵コンテのように、私はコードに固執する必要はありません。..スウィフト4:プロトコルへの「のViewController」の冗長準拠「UITableViewDataSource」

今日、私は使用してSearchViewControllerを作成このコード

@IBAction func showSearch(_ sender: Any) { 

    var resultvc = UITableViewController(style: .plain) 
    let searchvc = UISearchController(searchResultsController: self.resultvc) 
    self.present(searchvc, animated: true, completion: nil) 

} 

いいので、これは私が期待通りにボタンのすべての作品をクリックすると罰金ですが、searchResultsController

extension ViewController: UITableViewDataSource { 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath as IndexPath) 
     cell.textLabel!.text = "\(indexPath.row) - Its working" 
     return cell 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 2 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 2 
    } 
} 

// MARK: - Table View Delegate 
extension ViewController: UITableViewDelegate { 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let indexPath = tableView.indexPathForSelectedRow 
     let currentCell = tableView.cellForRow(at: indexPath!)! 
     print(currentCell.textLabel!.text!) 

    } 
} 
に何かを置くしようとしたとき

私が手:

「のViewController」の冗長準拠をプロトコルに「UITableViewDataSource」

は、あなたが登録する必要がストーリーボードの

+0

'class ViewController'の定義を表示してください - サブタイトルは' UITableViewController'か 'UIViewController'ですか? –

+0

self.resultvcとは何ですか? resultvcのローカル変数を作成しているときです。 –

+0

@ArunKumar、それはグローバルです、私はちょうどこの投稿の参照のためにそれを書いた –

答えて

0

なしresultTableViewで何かを置くためにあらゆる種類の方法があります識別子 "reuseIdentifier"のセル。これらのコード行を使用してください。

self.resultvc = UITableViewController(style: .plain) 
self.resultvc.tableView.dataSource = self 
self.resultvc.tableView.delegate = self 
self.resultvc.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier") 
let searchvc = UISearchController(searchResultsController: resultvc) 
self.present(searchvc, animated: true, completion: nil) 
+0

答えを感謝します、私は緑のチェックマークを与えることができますので、完全な答えを作るために完全なコードを使用してください、ありがとう。感謝! –

+0

ありがとう!早かった! –

+0

このソリューションは、「冗長適合」エラーメッセージと何が関係していますか? –

関連する問題