UIScrollView的不更新显示正确(UIScrollView not updating and

2019-09-18 08:10发布

林有我的问题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]);
    }
}

Answer 1:

这里有两点:

  1. 检查您的数据源。 如果图像已被正确地保存在你的文档目录与否。
  2. 无需再创建滚动视图和其父视图。 移动以下代码createScrollViewviewDidLoad 。 这些观点应创建只有一次。

self.slotBg = [[UIView的页头] initWithFrame:方法CGRectMake(43,370,300,143)]; CAGradientLayer *梯度= [CAGradientLayer层] gradient.frame = self.slotBg.bounds; gradient.colors = [NSArray的arrayWithObjects:(ID)[[的UIColor grayColor] CGColor],(ID)[[的UIColor whiteColor] CGColor],零]。 [self.slotBg.layer insertSublayer:梯度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在返回之前调用[自我setNeedsDisplay]。



文章来源: UIScrollView not updating and displaying properly
标签: iphone ios xcode