嗨,我遇到了一个简单的问题,我在视图中添加了一个uiscollview,并从一个数组中动态地向它添加了按钮,我正在从superview中删除它们,并在加载之前再次将它们添加到视图中,就像在代码中一样
- (void)loadGroups {
if(scroll!=nil){
if([scroll isDescendantOfView:self.view]){
[scroll removeFromSuperview];
}
}
NSUInteger n=[self.resultSet.dataObjectList count];
scroll = [[UIScrollView alloc]init];
scroll.delegate=self;
scroll.frame = CGRectMake(0, 0, 320, (height+25));
[scroll setContentSize:CGSizeMake((width-30)*n, height+25)];
[scroll setContentOffset:CGPointMake(0, 0) animated:NO];
scroll.backgroundColor=[UIColor clearColor];
//scroll.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GP-BG.png"]];
[scroll setCanCancelContentTouches:NO];
scroll.indicatorStyle=UIScrollViewIndicatorStyleWhite;
scroll.bounces=NO;
scroll.scrollEnabled=YES;
scroll.pagingEnabled=NO;
scroll.showsHorizontalScrollIndicator=NO;
for(int i=0;i<n;i++)
{
MODELRoom *room = [self.resultSet.dataObjectList objectAtIndex:i];
CGRect rectForTitleButton = CGRectMake(i*(width-30), 0, width-30, height+25);
int buttonTag =10000+i;
UIButton *titleButton = [self getButton:room tag:buttonTag ];
titleButton.tag=buttonTag;
[titleButton setFrame:rectForTitleButton];
titleButton.backgroundColor=[UIColor clearColor];
[scroll addSubview:titleButton];
CGRect rectForLabel = CGRectMake(i*(width-30), padding-5, width-35, height-20);
UILabel *label = [[UILabel alloc]init ];
label.frame=rectForLabel;
MODELRoom *room1 = [self.resultSet.dataObjectList objectAtIndex:i];
label.text=(room1).name;
label.textColor=[UIColor whiteColor];
label.textAlignment=NSTextAlignmentCenter;
label.backgroundColor=[UIColor clearColor];
label.font = [UIFont fontWithName:@"Helvetica" size:14];
[scroll addSubview:label];
CGRect rectForImageView = CGRectMake(27.5+(width-30)*i, padding+25, width-90, height-20);
UIImageView *imageView = [[UIImageView alloc]init ];
imageView.frame=rectForImageView;
imageView.backgroundColor=[UIColor clearColor];
imageView.image=[UIImage imageNamed:@"Group-icon.png"];
[scroll addSubview:imageView];
}
[self.view addSubview:scroll];
}我的问题是,当我在滚动后选择一个按钮时,它会重新统一到第一个按钮,但我需要在那里我选择了按钮。我哪里做错了请帮帮我。
提前感谢....
发布于 2013-04-23 13:13:52
尝试使用这个,我希望这将工作良好…
[scroll setContentOffset:CGPointMake(titleButton.frame.origin.x, titleButton.frame.origin.y) animated:YES];发布于 2013-04-23 13:08:32
一旦看到这张照片,
每当scollview的contentOffset值为(0,0)时,尝试更改该值,然后您将得到您的要求,
[scroll setContentOffset:CGPointMake(x-value, y-value) animated:NO];https://stackoverflow.com/questions/16161290
复制相似问题