pawisoon pawisoon - 8 months ago 57
Swift Question

How to handle tableview cell clicks and segues when delegate and datasource class is separate?

I tried to separate TableViewDelegate and TableViewDataSource to a separate class from ViewController and I'm facing a couple of problems now.

First problem:

App loads the tableview with all content but when I click on it or try to scroll all data disappears.

Second problem:
On click cell should link to another view where is more content displayed. I push data to this view using function. But when I separated the delegate and datasource to other class it doesnt work.

prepare(for segue: UIStoryboardSegue, sender: Any?)

Here is my code for view controller:

import UIKit
import Foundation
import os

class FirstViewController: UIViewController {

@IBOutlet weak var tableview: UITableView!
@IBOutlet weak var offlineModePicture: UIBarButtonItem!

@IBOutlet weak var refresh_button: UIBarButtonItem!

var wyznania_page = 0 // page
var isNewDataLoading = false

var wyznania = [[WyznanieData](),[WyznanieData](),[WyznanieData](),[WyznanieData](),[WyznanieData]()]

let activitiyViewController = ActivityViewController(message: "Ɓadowanie...

Answer Source

Try making your delegate variable global. it must be deallocation when goes out of scope in viewDidLoad (dataSource and delegate are weak in UITableView).

Extract following declaration global.

var customDelegate: TableViewDelegate!

then in viewDidLoad do following

customDelegate = TableViewDelegate(dataForRows: wyznania[wyznania_page])

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download