Hi fiends I'm new to ios app dev. I've struck with simple issue that in my app I added UIScrollView that scrolls horizontally. I've added buttons to that scrollview that will be dynamically changes. the code is
Step 1:
scroll = [[UIScrollView alloc]init];
scroll.delegate=self;
scroll.frame = CGRectMake(0, 0, 320, (height+25));
[scroll setContentSize:CGSizeMake((width-30)*n, height+25)];
scroll.backgroundColor=[UIColor clearColor];
scroll.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GP-BG.png"]];
scroll.showsHorizontalScrollIndicator=NO;
scroll.alwaysBounceHorizontal=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 getRoomButton:room tag:buttonTag];
titleButton.tag=buttonTag;
[titleButton setFrame:rectForTitleButton];
titleButton.backgroundColor=[UIColor clearColor];
[scroll addSubview:titleButton];
[controlButtons addObject:titleButton];
}
}
step 2:
- (UIButton*)getRoomButton:(MODELRoom *)currenRoom tag:(int)tagValue{
UIButton *button=[[UIButton alloc] init];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImageNormal;
if(currenRoom.currentlySelected){
buttonImageNormal=[UIImage imageNamed:[NSString stringWithFormat:@"GP-BG-Slected Green.png"]];
}else{
buttonImageNormal=[UIImage imageNamed:[NSString stringWithFormat:@""]];
}
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
[gridViewObjects addObject:button];
return button;
}
step 3:
- (void)buttonClicked:(UIButton *)sender{
if((int)[sender tag]>=10000 && (int)[sender tag]<20000){
currentRoom=((MODELRoom*)[resultSet.dataObjectList objectAtIndex:[sender tag]-10000]);
[self roomSelectionChanged:currentRoom];
currentRoom.currentlySelected=true;
scrollStrech=(width-30)*([sender tag]-10000);
scroll.contentOffset=CGPointMake(scrollStrech, 0);
NSLog(@"Scroll Strech=%d",scrollStrech);
for(int i=0;i<[resultSet.dataObjectList count];i++){
if(![[resultSet.dataObjectList objectAtIndex:i] isEqual:currentRoom]){
((MODELRoom*)[resultSet.dataObjectList objectAtIndex:i]).currentlySelected=false;
}
}
[self refresh];
}
step 4:
-(void)refresh{
if([self.scroll isDescendantOfView:self.view]){
[self.scroll removeFromSuperview];
}
}
Every thing is working fine but when I select the fifth or sixth button that is visible after scrolling and if I select that button the scroll view is rebouncing to first position. I found that this is coz of removing from superview and loading it again. if that the case also I need to show the scrollview region at that selected button region only.
kindly suggest me how to overcome this....