birdcage
birdcage

Reputation: 2676

Working multiple tableviews and datasources

I have 3 tableviews and three datasources. I fill them with the data from XML. When I fill only one tableview, all tableviews fill with the same text.

All tableviewcells have same identifier : "AlbumCell"

here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    otoFosilArray = [[NSMutableArray alloc] init];

    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    updateArray = appDelegate.derinlikListesiArrayMikro;

    _arrayFosiller1 = [[NSMutableArray alloc] init];
    _arrayFosiller2 = [[NSMutableArray alloc] init];
    _arrayFosiller3 = [[NSMutableArray alloc] init];

    tableFosiller1.dataSource = self;
    tableFosiller1.delegate = self;
    tableFosiller2.dataSource = self;
    tableFosiller2.delegate = self;
    tableFosiller3.dataSource = self;
    tableFosiller3.delegate = self;

    [self otoFosilEkle];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableFosiller1) {
        return 1;
    }
    else if (tableView == self.tableFosiller2) {
        return 1;
    }
    else if (tableView == self.tableFosiller3) {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (cbvMBentik.checked == YES) {
        return [_arrayFosiller1 count];
    }
    if (cbvMPlanktonik.checked == YES) {
        return [_arrayFosiller2 count];
    }
    if (cbvMDiger.checked == YES) {
        return [_arrayFosiller3 count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"AlbumCell"];
    if (cbvMBentik.checked == YES)
        cell.textLabel.text = [_arrayFosiller1 objectAtIndex:indexPath.row];
    if (cbvMPlanktonik.checked == YES)
        cell.textLabel.text = [_arrayFosiller2 objectAtIndex:indexPath.row];
    if (cbvMDiger.checked == YES)
        cell.textLabel.text = [_arrayFosiller3 objectAtIndex:indexPath.row];

    return cell;
}

- (void)otoFosilEkle
{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    otoFosilArray = appDelegate.derinlikListesiArrayMikroFosil;

    NSString *tempStr;

    for (int i = 0; i < [otoFosilArray count]; i++)
    {
        if ([[[otoFosilArray objectAtIndex:i] Durum]  isEqualToString:@"Bentik"]) {

            cbvMBentik.checked = YES;
            cbvMPlanktonik.checked = NO;
            cbvMDiger.checked = NO;
            tableFosiller1.userInteractionEnabled = YES;
            tableFosiller2.userInteractionEnabled = NO;
            tableFosiller3.userInteractionEnabled = NO;

            tempStr = @"text1";

            [_arrayFosiller1 addObject:tempStr];
            [tableFosiller1 reloadData];
        }
        if ([[[otoFosilArray objectAtIndex:i] Durum]  isEqualToString:@"Planktonik"]) {

            cbvMBentik.checked = NO;
            cbvMPlanktonik.checked = YES;
            cbvMDiger.checked = NO;
            tableFosiller1.userInteractionEnabled = NO;
            tableFosiller2.userInteractionEnabled = YES;
            tableFosiller3.userInteractionEnabled = NO;

            tempStr = @"text2";

            [_arrayFosiller2 addObject:tempStr];
            [tableFosiller2 reloadData];
        }
        if ([[[otoFosilArray objectAtIndex:i] Durum]  isEqualToString:@"Diğer"]) {

            cbvMBentik.checked = NO;
            cbvMPlanktonik.checked = NO;
            cbvMDiger.checked = YES;
            tableFosiller1.userInteractionEnabled = NO;
            tableFosiller2.userInteractionEnabled = NO;
            tableFosiller3.userInteractionEnabled = YES;

            tempStr = @"text3"

            [_arrayFosiller3 addObject:tempStr];
            [tableFosiller3 reloadData];
        }
    }
}

Upvotes: 0

Views: 1031

Answers (5)

Slashik
Slashik

Reputation: 325

You need three types of reusable cells in

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Create three identifiers and code for three different cell types in this method.

Upvotes: 1

Matt
Matt

Reputation: 2411

You should set a tag for each table view, then check these tags in a switch statement to determine which table view you're working with.

- (void)viewDidLoad
{
    ...
    self.tableFosiller1.tag = 1;
    self.tableFosiller2.tag = 2;
    self.tableFosiller3.tag = 3;
    ...
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger num = 0;
    switch(tableView.tag)
    {
       case 1:
       case 2:
       case 3:
         num = 1;
         break;
    }
    return num;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num = 0;
    switch(tableView.tag)
    {
       case 1:
         num = [_arrayFosiller1 count];
         break;
       case 2:
         num = [_arrayFosiller2 count];
         break;
       case 3:
         num = [_arrayFosiller3 count];
         break;
    }
    return num;    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = nil;
    switch(tableView.tag)
    {
       case 1:
         cellText = [_arrayFosiller1 objectAtIndex:indexPath.row];
         break;
       case 2:
         cellText = [_arrayFosiller2 objectAtIndex:indexPath.row];
         break;
       case 3:
         cellText = [_arrayFosiller3 objectAtIndex:indexPath.row];
         break;
    }

    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"AlbumCell"];
    if(!cell)
       cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"AlbumCell"];
    cell.textLabel.text = cellText;

    return cell;
}

As you can see, things can get very tricky when several table views are involved. If you're not entirely comfortable with that, I recommend using the free Sensible TableView framework. The framework simply works by taking your arrays and generating the table views. Really straight forward if you compare it with the above code.

Upvotes: 3

Lochana Ragupathy
Lochana Ragupathy

Reputation: 4320

if([tableView isEqual:self.tableFosiller1]){

  }
else if([tablView isEqual:self.tableFosiller2]){

   }
else if([tableView isEqual:self.tableFosiller3]){

    }

Just have a check in all tableView delegates method

Upvotes: 0

Girish
Girish

Reputation: 4712

All delegate methods, you need to check following condition

if (tableView == table1) {

}
else if (tableView == table2) {

}
else if (tableView == table3) {

}

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

You need to check

if (tableView == self.tableFosiller1) {

}
else if (tableView == self.tableFosiller2) {

}
else if (tableView == self.tableFosiller3) {

}

in all the delegate methods as you have same identifiers

Upvotes: 3

Related Questions