4

I have a UIViewController in which I've embedded a UITableView. If added an outlet to my ViewController class.

Because I don't want the ViewController to get too heavy, I would like to put the methods of the UITableViewDataSource protocol and UITableViewDelegate protocol into separate classes.

So I created a TableViewDataSource.swift and a TableViewDelegate class:

class TableViewDelegate : NSObject, UITableViewDelegate {
    //custom code here if required
}

class TableViewDataSource : NSObject, UITableViewDataSource {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return 1
    }
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier = "MyCellIdentifier"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

        cell.textLabel?.text = "left text"
        cell.detailTextLabel?.text = "right text"

        return cell


    }


}

class ViewController: UIViewController {

    @IBOutlet var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = TableViewDataSource()
        tableView.delegate = TableViewDelegate()

        tableView.setNeedsDisplay()

        // Do any additional setup after loading the view, typically from a nib.
    }
}

In my Storyboard, I've created a prototype cell within the tableView with the identifier "MyCellIdentifier". I use this identifier to create a cell in my TableViewDataSource method.

However, if I run the app, I only see an empty tableView. The methods don't even get called even though I have set them in the viewDidLoad method of the ViewController. I also tried to enforce the tableView to redraw by calling setNeedsDisplay. This had no effect, too.

enter image description here

productioncoder
  • 4,225
  • 2
  • 39
  • 65

1 Answers1

6

You're on the right track, and I believe your problem is easy to resolve. See here:

tableView.dataSource = TableViewDataSource()
tableView.delegate = TableViewDelegate()

That creates a new TableViewDataSource and a new TableViewDelegate and assigns them to dataSource and delegate respectively. Here are those properties on UITableView:

weak public var dataSource: UITableViewDataSource?
weak public var delegate: UITableViewDelegate?

Notice the weak bit? You're creating them and assigning them to a weak property, so they'll get tossed away pretty quickly.

Your solution is to create properties for those two new objects inside your view controller, then create those properties in viewDidLoad() and send those into the table view.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • Ok, now I see something. But there's something else. The text of the rightDetailLabel is empty even though I set it. The interesting thing is that the label actually exists and the text is set properly but that it is somehow not visible. – productioncoder Dec 19 '15 at 11:26
  • 1
    I'm glad the problem is resolved! Please mark it as correct so others can find it too. You should really post new questions as new questions otherwise others can't find them very easily. (But before you do, a brief tip: did you check your Auto Layout constraints to make sure both edges of the table are visible?) – TwoStraws Dec 19 '15 at 11:27