UIScrollView无法正确更新和显示

我在UIscrollView遇到问题,这就是我所做的:每当用户通过Imagepicker在相机卷中选择图像(多个或单个)时,我想在我的UIScrollView中显示它。 我能够显示它,但是当我再次访问Imagepicker再选择一个图像时,它不会更新UIScrollView ,我尝试将我的[self createScrollView]放在我的viewDidAppear但它重新创建了UIScrollView但没有更新它,所以旧图像和新图像组合在一起。 所以我把它们放在viewDidLoad但它只在我转到另一个View Controller然后再返回时更新。

//带有缩略图的我的UIScrollView

 - (void) createScrollView { self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 300, 143)]; CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = self.slotBg.bounds; gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; [self.slotBg.layer insertSublayer:gradient atIndex:0]; [self.view addSubview:self.slotBg]; self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,134.0f)]; [slotBg addSubview:self.scrollView]; int row = 0; int column = 0; for(int i = 0; i < _thumbs.count; ++i) { UIImage *thumb = [_thumbs objectAtIndex:i]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); [button setImage:thumb forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; button.tag = i; [scrollView addSubview:button]; if (column == 4) { column = 0; row++; } else { column++; } } [scrollView setContentSize:CGSizeMake(330, (row+1) * 60 + 10)]; } 

//在我的viewDidLoad中

 - (void)viewDidLoad { for(int i = 0; i <= 100; i++) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; NSLog(@"savedImagePath=%@",savedImagePath); if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; NSLog(@"file exists"); } } NSLog(@"Count : %d", [_images count]); [self createScrollView]; } 

编辑:

 - (void) viewDidLoad { [self createScrollView]; [_thumbs removeAllObjects]; UIView *removeView; for(int i = 0; i < _thumbs.count; ++i) { while((removeView = [scrollView viewWithTag:i]) != nil) { [removeView removeFromSuperview]; } { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; NSLog(@"savedImagePath=%@",savedImagePath); if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; NSLog(@"file exists"); } } NSLog(@"Count : %d", [_images count]); } } 

有两点:

  1. 检查您的数据源。 如果图像已正确保存在文档目录中。
  2. 无需再次创建滚动视图及其父视图。 将下面的代码从createScrollView移到viewDidLoad 。 这些视图应该只创建一次。

self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43,370,300,143)]; CAGradientLayer * gradient = [CAGradientLayer layer]; gradient.frame = self.slotBg.bounds; gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor],(id)[[UIColor whiteColor] CGColor],nil]; [self.slotBg.layer insertSublayer:gradient atIndex:0]; [self.view addSubview:self.slotBg];

 self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,134.0f)]; [slotBg addSubview:self.scrollView]; 

现在,从viewDidAppear开始,首先更新_thumbs数组(数据源)。 然后调用createScrollView函数。 在此函数内,首先使用UIScrollView中的标记删除所有子视图。 像你一样,再次添加所有的拇指。 然后在从createScrollView返回之前调用[self setNeedsDisplay]。