1

I'd like to get started using swift to make a small list based application. I was planning on using two table view controllers to display the two lists, and was wondering if it were possible to have them share a common data source.

Essentially the data would just be an item name, and two integers representing the amount of the item owned vs needed. When one number increases, the other decreases, and vice versa.

I figured this might be easiest to do using a single data source utilized by both table view controllers.

I did some googling on shared data sources and didn't find anything too useful to help me implement this. If there are any good references for me to look at please point me in their direction!

4
  • what's the difference between first and second table view controller ? You can use only one, and in cellForRowAtIndexPath method to check what cells you need to display for each cases in part Commented Aug 23, 2015 at 14:52
  • create as many table views as you want to and then just do things as you normally would but use a custom UITableViewCell and set a property for the custom cell to "owned" and "needed", in the tableview where you want to show the "owned" activate the owned property, in the tableview that you want to show the needed activate the needed property. subtract one from each other or repull from the server as necessary, that's it Commented Aug 23, 2015 at 15:00
  • Do you really want shared data source? You're introducing tight coupling between these view controllers that can cause maintenance issues in the future. You might share a model, but you generally wouldn't advise sharing data sources. Commented Aug 23, 2015 at 15:13
  • The only real difference between the first and second view controller would be the integer value being displayed in pair with the item name. For example: If an item is structured as so, (ItemName, Value1, Value2), then the first view controller would display: ItemName, Value2 while the second view controller would display: ItemName, Value1-Value2. Commented Aug 25, 2015 at 23:32

3 Answers 3

1

You can create one data source class and use it in both view controllers:

class Item {

}

class ItemsDataSource: NSObject, UITableViewDataSource {
    var items: [Item] = []

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        //setup cell
        // ...

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
}

class FirstViewController : UITableViewController {
    var dataSource = ItemsDataSource()

    override func viewDidLoad() {
        self.tableView.dataSource = dataSource
        self.tableView.reloadData()
    }
}

class SecondViewController : UITableViewController {
    var dataSource = ItemsDataSource()

    override func viewDidLoad() {
        self.tableView.dataSource = dataSource
        self.tableView.reloadData()
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

this is not a single data source, it is two data sources from a class with the same name
You are right. If author needs only one data source object he can use a singleton.
@EvgheniiTodorov do you work on freelance projects? If so, what's your hourly rate? Thanks
0

use singleton design pattern, it means both tables will get data source from the same instance

class sharedDataSource : NSObject,UITableViewDataSource{

    static var sharedInstance = sharedDataSource();
    override init(){
        super.init()
    }

    //handle here data source
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    }

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

    }


}

var tableOne = UITableView();
var tableTwo = UITableView();

tableOne.dataSource = sharedDataSource.sharedInstance;
tableTwo.dataSource = sharedDataSource.sharedInstance;

4 Comments

ugly solution to use singleton for providing data
@gottlieb explain why?
Singleton can be access and modified everywhere in your program, it's ok for an utility/configuration class but not for a data-providing class. It creates a hidden dependency between different INDEPENDENT program modules, which makes code harder to read and debug, and is a source of bugs.
I disagree, I think you are confusing between data provider and data storage. anyway if you have an article that approves that i'd like to read that
0

The first argument to the delegate method is:

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

At that point, your one Datasource delegate can decide which table view is wanting a cell, for example, and return results accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.