vendredi 20 mai 2016

Separating Data Source to another class in Swift and then handling data changes

I am trying to follow the patten seen here: http://ift.tt/1P3SuPY and have also checked out this question here: Separating Data Source to another class in Swift

I have the following code:

class ApplicationsViewController: UIViewController {

    @IBOutlet var tableView: UITableView!
    let tableViewDataSource = ApplicationTableDataSource()

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        setupView()
    }

    func setupView() {
        tableView.dataSource = tableViewDataSource
    }
}

However what I don't fully understand what is the best way to communicate / pass data into the data source.

As an example lets imagine that the app starts and we have no data in our data source. Inside our ApplicationTableDataSource class we have a method called setApplicationData. Inside the viewController we click an add button which adds a new application for a job:

func buttonPressed() {
    let data = NSData()        

    //Here we want to add the data to array in data source

    // We can't do the following as tableView.dataSource doesn't know about the setApplicationData method:
    tableView.dataSource.setApplicationData(data)

    // We could do: 
    tableViewDataSource.setApplicationData(data)
}

I am not sure if this is the best way to update the data source.

Is there a better way to handle updating the data source?

Aucun commentaire:

Enregistrer un commentaire