2016-02-10 16 views
9

Myślę, że to dość proste pytanie. Mam oddzielone moje UITableView źródeł delegat/dane do własnych rozszerzeńPrzekazanie UITableView za pomocą rozszerzeń swift

//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 
    } 
} 

Jednak w kontrolerze widoku sama muszę ustawić pełnomocnikowi tblView

class TweetsViewController : UIViewController { 

    @IBOutlet weak var tblView: UITableView! 


    var fetchedResultsController : NSFetchedResultsController! 

    //MARK: View Management 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tblView.dataSource = self 


    } 
} 

Jednakże, ponieważ widok jest kontroler ani zgodne z protokołami, ale posiadające rozszerzenia obsługują je, a następnie w jaki sposób jawnie ustawić źródło danych i delegować do tableView? Dzięki!

Odpowiedz

17

Można podzielić w rozszerzeniu, jak można sprawdzić w sekcji apple documentation o protokółach obsługi rozszerzeń.

Tutaj mam zaimplementować minimalny kod robiący to, o co prosisz, sprawdź to.

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

„kontroler widoku jest ani zgodne z protokołami ale posiadające rozszerzenia ich obsłużyć”

To jest błędne. Rozszerzenie powoduje, że kontroler widoku jest zgodny z protokołami, a źródło danych i delegat można ustawić jak zwykle, np .: self.tableView.delegate = self

4

W Swift 3 i powyżej widoku tabeli zmieniono metody źródła danych i delegatów.

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!) 

    } 
} 
Powiązane problemy