Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView delegate using extensions swift

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

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

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!

like image 791
zic10 Avatar asked Feb 10 '16 04:02

zic10


People also ask

What is UITableView delegate?

Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.

Which delegate method must be implemented to provide the cell of each row in a UITableView?

Delegate Methods in UITableView. willDisplay: This method gets called when the table view cell is going to display on the screen. willDisplayHeaderView: This method gets called when the header view of the section will be display.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What are delegate and DataSource methods of UITableView?

UITableview's Delegate Methods : The delegate of an UITableView object must adopt the UITableViewDelegate protocol. Optional methods of the protocol allow the delegate to manage selections, configure section headings and footers, help to delete and reorder cells, and perform other actions.


3 Answers

You can divide in a extension, as you can check in the apple documentation section 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
    }
}
like image 132
Ulysses Avatar answered Oct 07 '22 23:10

Ulysses


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

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

  }
}
like image 30
Piyush Mathur Avatar answered Oct 07 '22 22:10

Piyush Mathur


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

like image 7
Andreas Avatar answered Oct 08 '22 00:10

Andreas