UIScrollView删除无法正常工作

时间:2011-06-02 23:57:16

标签: objective-c uiscrollview

我有一个启用了分页的UIScrollView等...我在UIImageView的顶部添加了一个删除按钮,它被添加到UIScrollView中。但是,当我从库中添加三个图像到scrollView,然后通过按删除按钮删除所有图像,它可以正常工作。但是当我再次执行相同的过程时,即使pageControl的numberofpages为0,它仍会在UIScrollView上保留UIImageViews。

请你告诉我我做错了什么:

- (void) updateScrollView {
    CGRect currentFrame = self.scrollView.frame;
    currentFrame.origin.x = 0;
    numberOfPages = [[self selectedImages] count];
    if ([self.selectedImages count] > 0) {
    for (UIImage *image in [self selectedImages]) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];
            [btn setFrame:CGRectMake(0, 0, 30, 30)];
            [btn setTag:[[self selectedImages] indexOfObject:image]];
            [btn addTarget:self action:@selector(removeImageView:) forControlEvents:UIControlEventTouchUpInside];
            UIImageView *imagev = [[UIImageView alloc] initWithImage:image];
            [imagev setContentMode:UIViewContentModeScaleAspectFit];
            [imagev setFrame:currentFrame];
            [imagev addSubview:btn];
            [imagev setUserInteractionEnabled:YES];
            [self.scrollView addSubview:imagev];
            [imagev release];
            currentFrame.origin.x = currentFrame.origin.x + currentFrame.size.width;
        }
    }
    else {
        [self.selectedImages removeAllObjects];
        for (int i = 0; i < [self.scrollView.subviews count]; i++) {
            [[self.scrollView.subviews objectAtIndex:i] removeFromSuperview];
        }
        return;
    }
    self.pageControl.numberOfPages = [self.selectedImages count];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setContentSize:CGSizeMake(currentFrame.origin.x, currentFrame.size.height)];
}
- (void) removeImageView:(UIButton *)sender {
    if ([self.selectedImages count] >= 1) {
        int i = sender.tag;
        id superv = [sender superview];
        [superv removeFromSuperview];
        [self.selectedImages removeObjectAtIndex:i];
        [self updateScrollView];
    }
    else {
        [self.selectedImages removeAllObjects];
    }
    return;
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    currentPage = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    [self.pageControl setCurrentPage:currentPage];
    return;
}
- (void) updatePageControl:(id)sender {
    UIPageControl *pg = (UIPageControl *)sender;
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * pg.currentPage;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:YES];
}
- (void) removeObject:(id)object {
    [self.selectedImages removeObject:object];
    [self updateScrollView];
    [self updatePageControl:self.pageControl];
    CGFloat pageWidth = self.scrollView.frame.size.width;
    currentPage = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    [self.pageControl setCurrentPage:currentPage];

    return;
}

1 个答案:

答案 0 :(得分:0)

删除第一个图像视图后,您正在调用updateScrollView并且还剩2个图像。在updateScrollView中,首先如果条件将评估为YES,并且您将添加另外两个图像以滚动视图,那么现在,有2对图像。当删除下一个图像时,删除两个图像并再添加4个图像。

另外,请注意UIScrollView会在子视图数组中保留滚动条,所以这不好。

 for (int i = 0; i < [self.scrollView.subviews count]; i++) {
            [[self.scrollView.subviews objectAtIndex:i] removeFromSuperview];
}

在继续之前,您应该优化此代码。在每个方法上放置断点或只是NSLog(@“%s”,__ FUNCTION __); (在每个方法的开头删除下划线和FUNCTION之间的空格!)并了解什么时候和由谁调用。当您开始了解代码的工作原理时,原始问题的解决方案将无处不在。不幸的是,我无法帮助你,因为你忘了提及你是如何“再次”做同样的过程。