UIScrollView与分页无法正常工作

时间:2011-10-13 13:36:56

标签: iphone xcode4 uiscrollview uipagecontrol

我已经实现了一个带分页的scrollview,但它无法正常工作。对于scrollview的每个视图,我必须使用某些值填充tableview。即使值正确,所有视图都是相同的(最后一个视图是scrollview的每个子视图上的视图)我的错误在哪里?

这是我的代码:

- (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];

}

其中gammeString,usageString,delaiString,typeString,financementString包含将设置为tableview单元格上某些标签的文本。

这是我的cellForRowAtIndexPath方法:

- (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;
}

2 个答案:

答案 0 :(得分:1)

- (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"];

}

使用此代码替换您的代码,您肯定会获得解决方案。

答案 1 :(得分:0)

我找到了我的问题的解决方案:我使用NSMutableArray而不是NSString,它可以正常工作。