Bryan
Bryan

Reputation: 1375

UITableView with Multiple Data Arrays

I have an app, in which I have 7 different UITableViewControllers. All 7 are linked through a tabBarController. I am looking for a way to have a single custom class to be used throughout all 7 UITableViewControllers. I have 7 different arrays that all hold a specific number of objects. I need to know how to:

I'm familiar with using UITableView with a single data source, but I really don't know how to approach it with multiple data sources.

Thanks!

Upvotes: 2

Views: 1852

Answers (2)

0yeoj
0yeoj

Reputation: 4550

• Change the number of rows in the tableView, depending on the array that I'm using as my data source.

You can accomplish this by conditions in tableView delegates

- (NSInteger)tableView:tableView numberOfRowsInSection:section

Inside this delegate you need to identify which dataSource for the particular tableView.

Check the table if its the one being refreshed like so:

- (NSInteger)tableView:tableView numberOfRowsInSection:section
{
    if (tableView == self.firstTableView)
        return self.firstTableDataSource.count;

    if (tableView == self.secondTableView)
        return self.secondTableDataSource.count;

    //and so on..
}

• Change the array that is being used as the data source based on which ViewController the user is currently looking at (Can this even be done?)

Figuring which array you will be using for that particular table is up to you. You can use segement control, buttons, another table, it's up to you.

But the very important part is [tableView reloadData]; at your target table (table that is currently active) and again table delegates will be triggered and you will be doing all the filtering inside those delegates..

while you can check if the viewController is visible by:

if ([self/*viewController*/ isViewLoaded] && self/*viewController*/.view.window)
{
    //visible
} 

which was already discussed here

• Change the contents of a cell, based on the array being used as the data source.

This one is not clear. Is it just the content/values of the subviews of the cell like: cell.textLabel, cell.detailTextLabel and cell.imageView?

or the cell.contentView which is basically, you want to change the look of your cell?

If content/values again you just have to determine which is which, like this (using customCell):

assuming you have a dataSource that looks like:

{
    data_source = (
            {
                text_label = test0;
                detail_label = "this is just a text";
                image_name = "your_image0.png";
            },
            {
                text_label = test1;
                detail_label = "this is just a another text";
                image_name = "your_image1.png";
            }
        )
}

then in the delegate cellForRowAtIndexPath it'll be something like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"tableID";

    self.customCell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!self.customCell)
        self.customCell = [[YourCustomCell alloc] initWithStyle:(UITableViewCellStyle) reuseIdentifier:cellID];

    static NSString *dataSource = @"data_source";
    static NSString *textLabel = @"text_label";
    static NSString *detailLabel = @"detail_label";
    static NSString *imageName = @"image_name";

    if (tableView == self.firstTableView)
    {
        self.customCell.textLabel.text = [self.firstDataSource valueForKey:dataSource][indexPath.row][textLabel];
        self.customCell.detailTextLabel.text = [self.firstDataSource valueForKey:dataSource][indexPath.row][detailLabel];
        self.customCell.imageView.image = [UIImage imageNamed:[self.firstDataSource valueForKey:dataSource][indexPath.row][imageName]];
    }

    if (tableView == self.secondTableView)
    {
        self.customCell.textLabel.text = [self.secondDataSource valueForKey:dataSource][indexPath.row][textLabel];
        self.customCell.detailTextLabel.text = [self.secondDataSource valueForKey:dataSource][indexPath.row][detailLabel];
        self.customCell.imageView.image = [UIImage imageNamed:[self.secondDataSource valueForKey:dataSource][indexPath.row][imageName]];
    }
    // and so on...
}

To check all other methods, check apples documentation ,i hope this is useful for you and for others as well.. Happy coding.. :)

Upvotes: 1

JordanC
JordanC

Reputation: 1323

You can have one class be the dataSource for all of the UITableViewControllers

You might implement this by creating a custom subclass of UITabBarController which keeps an array of UITableViewControllers and a corresponding dictionary that maps a UITableVC to the array used by it's data source.

Set that as the data source for all the UITableViews and then handle each dataSource method like my example below.

Take a look at the UITableViewDataSource docs. All of the methods pass in which tableView they're trying to get information about.

For example:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Here you could compare the tableView var to your array of tableViews to figure out which table view called this
    //Based on that you could query your dictionary to find the array that houses the data for that tableView.
    //Use the indexPath to find the data that you need to create and return the right cell
}

Upvotes: 4

Related Questions