我有一个从表视图调用的模态视图。每当我调用模态视图时,我会在模态视图中设置一个小视图,其中包含一些可滚动的图片。问题是,当我回到表格视图并选择表格的另一行时,旧图像应该消失,而新图像则会出现,但这种情况不会发生。好像当我第一次加载模态视图时,它填充了最初的2个图片,当我再次加载它时,它会不断添加旧图片的新图片..
以下是代码:
- (void)layoutScrollImages {
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
//NSMutableArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake(([self.nomeFotos count] * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
和
- (void)setScrollingImages {
//self.view.backgroundColor = [UIColor viewFlipsideBackgroundCol];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= [self.nomeFotos count]; i++)
{
NSString *imageName = [NSString stringWithFormat:[self.nomeFotos objectAtIndex:(i-1)]];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSLog(@"%@", imageName);
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
答案 0 :(得分:0)
我只是想想我想要的,一个解决方案是删除所有当前的子视图,然后添加新的视图..代码如下:
for (UIView* subView in [self.view.subviews]) {
[subView removeFromSuperview];
}
我将此视图放在我的视图中,每次出现视图时,它都会清除其子视图,并且我会调用方法添加新的子视图(图片)。
由于