使用扩展名swift的UITableView委托

zic*_*c10 18 uitableview ios swift

我认为这是一个相当简单的问题.我已经将我的UITableView委托/数据源分成了自己的扩展

//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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在视图控制器本身我需要设置tblView委托

class TweetsViewController : UIViewController {

    @IBOutlet weak var tblView: UITableView!


    var fetchedResultsController : NSFetchedResultsController!

    //MARK: View Management

    override func viewDidLoad() {
        super.viewDidLoad()

        tblView.dataSource = self


    }
}
Run Code Online (Sandbox Code Playgroud)

但是,由于视图控制器既不符合协议,但有扩展处理它们,那么如何显式设置tableView的数据源和委托?谢谢!

Uly*_*ses 29

您可以划分扩展名,因为您可以在Apple文档部分中查看有关扩展处理协议的信息.

在这里,我实现了你所要求的最小代码,检查出来.

  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
    }
}
Run Code Online (Sandbox Code Playgroud)


Piy*_*hur 7

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

  }
}
Run Code Online (Sandbox Code Playgroud)


And*_*eas 6

视图控制器既不符合协议,也具有扩展处理协议

这是不正确的.扩展使视图控制器符合协议,数据源和委托可以像往常一样设置,例如:self.tableView.delegate = self