2016-02-10 1 views
9

これはかなり簡単な質問です。ビューコントローラがあるので、私は、拡張機能を使用するUITableViewデリゲート

class TweetsViewController : UIViewController { 

    @IBOutlet weak var tblView: UITableView! 


    var fetchedResultsController : NSFetchedResultsController! 

    //MARK: View Management 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tblView.dataSource = self 


    } 
} 

しかし、私はtblViewデリゲートを設定する必要がビューコントローラに

//MARK: - UITableView Data Source/Delegate 

extension TweetsViewController: UITableViewDataSource { 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 0 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TweetCell 
     return cell 
    } 
} 

しかし、独自の拡張機能の中に自分自身を私ののUITableViewデリゲート/データソースを分離しましたプロトコルにも準拠していませんが、拡張機能でそれらを処理すると、明示的にデータソースを設定してtableViewのデリゲートをどのように行うのですか?ありがとう!

答えて

17

apple documentationセクションのプロトコルの処理に関するセクションをチェックすることができますので、内線で分けることができます。

ここでは、尋ねることを行う最小限のコードを実装しています。チェックアウトしてください。

import UIKit 


class TableViewViewController: UIViewController { 
    @IBOutlet weak var table: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     table.delegate = self 
     table.dataSource = self 
    } 
} 

extension TableViewViewController: UITableViewDelegate,UITableViewDataSource { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
     cell.textLabel!.text = "it works" 
     return cell 
    } 

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 
} 
3

は、「View Controllerがや拡張がそれらを扱うプロトコルに準拠するが、持っている」

これは正しくありません。この拡張により、View Controllerはプロトコルに準拠し、データソースとデリゲートは通常どおり設定できます(例:self.tableView.delegate = self

4

)Swift 3以上では、テーブルビューのデータソースとデリゲートメソッドが変更されました。

import UIKit 

class HomeViewController: UIViewController { 

    @IBOutlet var tblPropertyList: UITableView! 
    // MARK: - View Life Cycle 
    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
    tblPropertyList.delegate = self 
    tblPropertyList.dataSource = self 
    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 
} 

// MARK: - Table View DataSource 
extension HomeViewController: 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 HomeViewController: UITableViewDelegate { 

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

    } 
} 
関連する問題