2016-02-10 58 views
9

这是一个相当简单的问题,我认为。我已经分开我的UITableView委托/数据源到自己的扩展使用扩展的UITableView委托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 
    } 
} 

然而,在视图控制器本身我需要设置tblView委托

class TweetsViewController : UIViewController { 

    @IBOutlet weak var tblView: UITableView! 


    var fetchedResultsController : NSFetchedResultsController! 

    //MARK: View Management 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tblView.dataSource = self 


    } 
} 

然而,由于该视图控制器也不符合协议,但有扩展名处理它们,那么我该如何显式设置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

“的视图控制器也不符合协议,但具有扩展处理它们”

这是不正确。该扩展使视图控制器符合协议,数据源和委托可以照常设置,例如: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!) 

    } 
}