ios UITableView 委托使用扩展 swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35306862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:27:22  来源:igfitidea点击:

UITableView delegate using extensions swift

iosswiftuitableview

提问by zic10

This is a fairly simple question I think. I've separated my UITableViewdelegate / data sources into their own extensions

我认为这是一个相当简单的问题。我已经将我的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
    }
}

However in the view controller itself I need to set the tblView delegate

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

class TweetsViewController : UIViewController {

    @IBOutlet weak var tblView: UITableView!


    var fetchedResultsController : NSFetchedResultsController!

    //MARK: View Management

    override func viewDidLoad() {
        super.viewDidLoad()

        tblView.dataSource = self


    }
}

However, since the view controller is nor conforming to the protocols but having the extensions handle them, then how do I explicitly set the datasource and delegate for the tableView? Thanks!

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

回答by Ulysses

You can divide in a extension, as you can check in the apple documentationsection about Extensions handling Protocols.

您可以划分一个扩展,因为您可以查看有关扩展处理协议的苹果文档部分。

Here I have implement a minimum code doing what you ask, check it out.

在这里,我已经实现了一个最低限度的代码,可以按照您的要求进行操作,请查看。

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

回答by Piyush Mathur

In Swift 3 and above the table view datasource and delegate methods changed.

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

  }
}

回答by Andreas

the view controller is nor conforming to the protocols but having the extensions handle them

视图控制器也不符合协议,但让扩展处理它们

This is incorrect. The extension makes the view controller conformant to the protocols, and the data source and delegate can be set as usual, e.g.: self.tableView.delegate = self

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

回答by EddieG

Now in Swift 5.1 you don't need to inherit UITableViewDelegate and UITableViewDataSource

现在在 Swift 5.1 你不需要继承 UITableViewDelegate 和 UITableViewDataSource

extension HomeViewController {

// MARK: - Table View DataSource

  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

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

  }
}