Gabrielle
Gabrielle

Reputation: 4981

UIScrollView with paging not working ok

I have implement a scrollview with paging but it not work ok. For every view of the scrollview I must populate a tableview with certain values. Even if the values are correct received all the views are identical (the last view is the view on every subview of scrollview) Where is my mistake?

Here is my code :

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.vehiculesenvisagesScrollView.pagingEnabled = YES;
    self.vehiculesenvisagesScrollView.contentSize = CGSizeMake(vehiculesenvisagesScrollView.frame.size.width * [vehiculesEnvisages count], vehiculesenvisagesScrollView.frame.size.height);
    self.vehiculesenvisagesScrollView.showsHorizontalScrollIndicator = NO;
    self.vehiculesenvisagesScrollView.showsVerticalScrollIndicator = NO;
    self.vehiculesenvisagesScrollView.scrollsToTop = NO;


    vehiculesEnvisagesArray = [[NSMutableArray alloc] initWithObjects:@"Gamme", @"Usage",@"Délai d'echart",@"Type d'achart",@"Financement", nil];


    for (int i=0; i<[vehiculesEnvisages count];i++){
        [self loadScrollViewWithPage:i];
    }

     NSLog(@"views %@",[vehiculesenvisagesScrollView subviews]);

    self.pageControlVehiculeEnvisages.numberOfPages=[vehiculesEnvisages count];
    self.pageControlVehiculeEnvisages.currentPage=0;    
}

- (void) loadScrollViewWithPage: (int) page {

    NSLog(@"%d",page);
    if (page < 0) return;
    if (page >= [vehiculesEnvisages count]) return;

    CGRect frame;
    frame.origin.x = self.vehiculesenvisagesScrollView.frame.size.width * page;
    frame.origin.y = 0;
    frame.size = self.vehiculesenvisagesScrollView.frame.size;

    UIView *oneview = [[UIView alloc] initWithFrame:frame]; 

    tableViewVehiculesEnvisages=[[UITableView alloc] initWithFrame:CGRectMake(3, 0, 320, 233) style:UITableViewStyleGrouped];
    tableViewVehiculesEnvisages.bounces=NO;
    tableViewVehiculesEnvisages.backgroundColor=[UIColor clearColor];
    [tableViewVehiculesEnvisages setDelegate:self];
    [tableViewVehiculesEnvisages setDataSource:self];        

    self.gammeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"gamme"];
    self.usageString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"usage"];
    self.delaiString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"delai"];
    self.typeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"achat"];
    self.financementString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"financement"];

    [oneview addSubview:tableViewVehiculesEnvisages];

    NSLog(@"gamme%@", self.gammeString);
    [self.vehiculesenvisagesScrollView addSubview:oneview];
    [oneview release];

}

where gammeString,usageString,delaiString,typeString,financementString contains the text that will be set to some labels on a cell of tableview.

Here is my cellForRowAtIndexPath method :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];

        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.backgroundView.opaque = NO;

        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.opaque = NO;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:15];

        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.opaque = NO;
        cell.detailTextLabel.textColor = [UIColor whiteColor];
        cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];

        if (indexPath.row==0)
        {
            gammeLabel = [[UILabel alloc] init];
            gammeLabel.frame = CGRectMake(150, -4, 150, 50);
            gammeLabel.textColor=[UIColor grayColor];
            gammeLabel.backgroundColor=[UIColor clearColor];
            gammeLabel.text = self.gammeString;  
            [cell.contentView addSubview: [gammeLabel autorelease]];   
        }

        if(indexPath.row==1)
        {
            usageLabel = [[UILabel alloc] init];
            usageLabel.frame = CGRectMake(150, -4, 150, 50);
            usageLabel.textColor=[UIColor grayColor];
            usageLabel.backgroundColor=[UIColor clearColor];
            usageLabel.text = self.usageString;
            [cell.contentView addSubview: [usageLabel autorelease]];
        }

        if(indexPath.row==2)
        {
            delaiLabel = [[UILabel alloc] init];
            delaiLabel.frame = CGRectMake( 150, -4, 150, 50);
            delaiLabel.textColor = [UIColor grayColor];
            delaiLabel.backgroundColor = [UIColor clearColor];
            delaiLabel.text = self.delaiString;
            [cell.contentView addSubview: [delaiLabel autorelease]];

        }

        if(indexPath.row==3)
        {
            typeLabel = [[UILabel alloc] init];
            typeLabel.frame = CGRectMake( 150, -4, 150, 50);
            typeLabel.textColor = [UIColor grayColor];
            typeLabel.backgroundColor = [UIColor clearColor];
            typeLabel.text =self.typeString;
            [cell.contentView addSubview: [typeLabel autorelease]];

        }

        if(indexPath.row==4)
        {
            financementLabel = [[UILabel alloc] init];
            financementLabel.frame = CGRectMake( 150, -4, 150, 50);
            financementLabel.textColor = [UIColor grayColor];
            financementLabel.backgroundColor = [UIColor clearColor];
            financementLabel.text = self.financementString;
            [cell.contentView addSubview: [financementLabel autorelease]];

        }

    }
    [[cell textLabel] setText: [vehiculesEnvisagesArray objectAtIndex:indexPath.row]] ;

    return cell;
}

Upvotes: 0

Views: 411

Answers (2)

Gabrielle
Gabrielle

Reputation: 4981

I found the solution of my issue : I use a NSMutableArray instead of a NSString and it works ok.

Upvotes: 0

Dhaval
Dhaval

Reputation: 168

- (void) loadScrollViewWithPage: (int) page {

NSLog(@"%d",page);
if (page < 0) return;
if (page >= [vehiculesEnvisages count]) return;

CGRect frame;
frame.origin.x = self.vehiculesenvisagesScrollView.frame.size.width * page;
frame.origin.y = 0;
frame.size = self.vehiculesenvisagesScrollView.frame.size;

UIView *oneview = [[UIView alloc] initWithFrame:frame]; 

tableViewVehiculesEnvisages=[[UITableView alloc] initWithFrame:CGRectMake(3, 0, 320, 233) style:UITableViewStyleGrouped];
tableViewVehiculesEnvisages.tag=page;
tableViewVehiculesEnvisages.bounces=NO;
tableViewVehiculesEnvisages.backgroundColor=[UIColor clearColor];
[tableViewVehiculesEnvisages setDelegate:self];
[tableViewVehiculesEnvisages setDataSource:self];        

 [oneview addSubview:tableViewVehiculesEnvisages];

NSLog(@"gamme%@", self.gammeString);
[self.vehiculesenvisagesScrollView addSubview:oneview];
[oneview release];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
}

    [self setTextt:tableView.tag];

    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.backgroundView.opaque = NO;

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.opaque = NO;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];

    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.opaque = NO;
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];

    if (indexPath.row==0)
    {
        gammeLabel = [[UILabel alloc] init];
        gammeLabel.frame = CGRectMake(150, -4, 150, 50);
        gammeLabel.textColor=[UIColor grayColor];
        gammeLabel.backgroundColor=[UIColor clearColor];
        gammeLabel.text = self.gammeString;  
        [cell.contentView addSubview: [gammeLabel autorelease]];   
    }

    if(indexPath.row==1)
    {
        usageLabel = [[UILabel alloc] init];
        usageLabel.frame = CGRectMake(150, -4, 150, 50);
        usageLabel.textColor=[UIColor grayColor];
        usageLabel.backgroundColor=[UIColor clearColor];
        usageLabel.text = self.usageString;
        [cell.contentView addSubview: [usageLabel autorelease]];
    }

    if(indexPath.row==2)
    {
        delaiLabel = [[UILabel alloc] init];
        delaiLabel.frame = CGRectMake( 150, -4, 150, 50);
        delaiLabel.textColor = [UIColor grayColor];
        delaiLabel.backgroundColor = [UIColor clearColor];
        delaiLabel.text = self.delaiString;
        [cell.contentView addSubview: [delaiLabel autorelease]];

    }

    if(indexPath.row==3)
    {
        typeLabel = [[UILabel alloc] init];
        typeLabel.frame = CGRectMake( 150, -4, 150, 50);
        typeLabel.textColor = [UIColor grayColor];
        typeLabel.backgroundColor = [UIColor clearColor];
        typeLabel.text =self.typeString;
        [cell.contentView addSubview: [typeLabel autorelease]];

    }

    if(indexPath.row==4)
    {
        financementLabel = [[UILabel alloc] init];
        financementLabel.frame = CGRectMake( 150, -4, 150, 50);
        financementLabel.textColor = [UIColor grayColor];
        financementLabel.backgroundColor = [UIColor clearColor];
        financementLabel.text = self.financementString;
        [cell.contentView addSubview: [financementLabel autorelease]];

    }

[[cell textLabel] setText: [vehiculesEnvisagesArray objectAtIndex:indexPath.row]] ;

return cell;
}   



-(void)setTextt:(int)page
{
self.gammeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"gamme"];
self.usageString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"usage"];
self.delaiString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"delai"];
self.typeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"achat"];
self.financementString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:@"financement"];

}

Replace your code with this code,You defiantly get solution.

Upvotes: 1

Related Questions