嗨,我遇到了一个简单的问题,即我添加了一个 uiscollview 来查看并从数组中动态地添加按钮,我将它们从超级视图中删除,并在加载之前再次将它们添加到视图中,如代码所示
- (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];
}
我的问题是,当我在滚动后选择一个按钮时,它会重新调整到第一个按钮,但我需要在我选择按钮的地方。请帮助我在哪里做错了......
提前致谢....