uiscrollview 删除子view时,继续滚动到被删除的索引。

问题:我在做一个相册的时候,如果删除uiscrollerview的元素,假如原本scroll的元素为9,然后我 删除1个,元素,接着把9个元素都删除掉,再重新添加8个元素,结果scroller在滚动时也可以滚动到第9个位置上。这是如果删除第9个元素,会报错,解决办法是动态的设置uisrollerview的contentSize。这样就可以了。

例子代码如下

 1 //更新相片
 2 -(void)UpdatePhoto
 3 {
 4       NSLog(@"imageNameCount:%d---scrollIndex:%d",self.imageNameArray.count,scrollIndex);
 5     for(int i = 0;i<self.imageNameArray.count;i++)
 6     {
 7         UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(1024*i, 0, 1024, 700)];
 8         bgView.backgroundColor = [UIColor blackColor];
 9         
10         UIImageView *photoView = [[UIImageView alloc]init];
11         photoView.image = [self.bigImageArray objectAtIndex:i];
12         photoView.frame = CGRectMake(0, 0, photoView.image.size.width, photoView.image.size.height);
13         photoView.center = CGPointMake(512, 359);
14         
15         [bgView addSubview:photoView];
16         [photoScrollView addSubview:bgView];
17     }
//添加这一句就行了。
18 self.photoScrollView.contentSize = CGSizeMake(1024*self.imageNameArray.count, 700); 19 20 CGRect Positionframe = CGRectMake(1024*scrollIndex, 0, 1024, 700); 21 [self.photoScrollView scrollRectToVisible:Positionframe animated:NO]; 22 }

 

posted on 2013-02-18 10:22  wtq  阅读(1481)  评论(0编辑  收藏  举报