brmcdani44
brmcdani44

Reputation: 27

2 Table Views on One Page - Two NSArrays

Hello I am brand new to xCode and iPhone development. I have two different TableView controls on one page. I have NSArray #1 that needs to be the datasource of TableView #1 and NSArray #2 that needs to be the datasource for TableView #2.

The only trouble is that NSArray#1 populates both TableView1 and TableView2. I looked through the code and can't seem to find where you can distinguish which NSArray belongs to each TableView.

Any help will be appreciated. Thanks in advance!

@interface GrainBinContentsEstimatorViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *tableViewGrainBinType;
    UITableView *tableViewGrainType;
}

@property (strong, nonatomic) NSArray *arrayGrainBinTypes;
@property (strong, nonatomic) NSArray *arrayGrainTypes;
@property (nonatomic, retain) UITableView *tableViewGrainBinType;
@property (nonatomic, retain) UITableView *tableViewGrainType;

@implementation GrainBinContentsEstimatorViewController
@synthesize arrayGrainBinTypes, arrayGrainTypes, tableViewGrainBinType, tableViewGrainType;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.arrayGrainBinTypes = [[NSArray alloc]
                       initWithObjects:@"RF", @"RC45", @"RC60", nil];

    self.arrayGrainTypes = [[NSArray alloc]
                            initWithObjects:@"Wheat", @"Corn", @"Soybeans", @"Rice", @"Milo", @"Barley", nil]; 
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"Select Bin Type";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayGrainBinTypes count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [self.arrayGrainBinTypes objectAtIndex: [indexPath row]];
    return cell;
}

Upvotes: 0

Views: 260

Answers (2)

jmstone617
jmstone617

Reputation: 5707

First of all, welcome to SO.

So, you have a reasonable design concept here: One array populates one table view. The data source/delegate of your table view is typically a UITableViewController or UIViewController, but it certainly doesn't have to be. In your case, it's your UIViewController. So, what happens is that when each table view loads, it asks its data source "Hey, how many rows/sections do I have? What do my cells look like?" and other questions.

In your case, you're not distinguishing between table views! So tableView1 is asking "what do my cells look like? Let's see what tableView:cellForRowAtIndexPath: has to say!" and you're giving it info from your arrayGrainByTypes array. Then tableView2 comes along and asks the same question, and uses the same method to get its answer. In each of these data source methods, you are typically given, as an argument to the method, which table view is asking for this information. So, just check which table view is asking!

if (tableView == self.tableViewGrainType) {
    // table view 1's information is set here
else if (tableView == self.tableViewGrainBinType) {
    // table view 2's information is set here
}

Upvotes: 1

Alladinian
Alladinian

Reputation: 35696

In every delegate method (callback) the caller (tableview) is passed as a parameter. So you can switch based on this parameter like:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   if (tableView == self.tableViewGrainBinType) return [self.arrayGrainBinTypes count];
   else return [self.arrayGrainTypes count];
}

You get the idea...

Upvotes: 1

Related Questions