UIScrollView 分页无法正常工作
我已经实现了带有分页的滚动视图,但它不能正常工作。对于滚动视图的每个视图,我必须使用某些值填充表视图。即使收到的值正确,所有视图都是相同的(最后一个视图是滚动视图的每个子视图上的视图)我的错误在哪里?
这是我的代码:
- (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
包含将设置为表视图单元格上的某些标签的文本。
这是我的 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用这段
代码替换你的代码,你肯定会得到解决方案。
}
Replace your code with this code,You defiantly get solution.
我找到了问题的解决方案:我使用 NSMutableArray 而不是 NSString 并且它工作正常。
I found the solution of my issue : I use a NSMutableArray instead of a NSString and it works ok.