简体   繁体   English

iOS - UIScrollView不会更新其内容,并且一些UIViews在重新加载后会消失

[英]iOS - UIScrollView doesn't update its content and some UIViews disappear after reload

I have UITableViewController with UIScrollView inside UIView as its header. UITableViewControllerUIScrollView里面UIView作为它的头。 UIScrollView is used to display slideshow of images: UIScrollView用于显示图像的幻灯片:

UITableView
 -UITableViewHeaderView
   -UIView
      -UIScrollView
        -UIImageView
          ...
      -UIPageControl

Every image has a subview with its title and description: 每个图像都有一个带有标题和描述的子视图:

UIImageView
  -UIView
    -UILabel
    -UILabel

When I need to update my tableView a delegate method is called which reloads data in a tableView and calls addImages method: 当我需要更新我的tableView会调用一个委托方法,该方法在tableView重新加载数据并调用addImages方法:

- (void)eventsUpdated
{
    if ([self respondsToSelector:@selector(setRefreshControl:)])
        [self.refreshControl endRefreshing];

    [self.tableView reloadData];
    [self addImages];
}

Here is how I add my images: 以下是我添加图片的方式:

- (void)addImages
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    for (int i = 0; i < images.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIImageView *subview = [self createImageViewForEvent:[images objectAtIndex:i] inFrame:frame];
        subview.frame = frame;
        [self.scrollView addSubview:subview];
    }

    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*images.count, scrollView.frame.size.height);
    pageControll.currentPage = 0;
    pageControll.numberOfPages = images.count;

}

- (UIImageView *)createImageViewForEvent:(Event *)event inFrame:(CGRect)frame
{
    UIImage *image;

    NSString *imageName = [event.imageName lastPathComponent];
    NSString *bundleFilePath = [[NSBundle mainBundle] pathForResource:[imageName stringByDeletingPathExtension] ofType:[imageName pathExtension]];

    image = [UIImage imageWithContentsOfFile:bundleFilePath];


    UIImageView *output = [[UIImageView alloc] initWithImage:image];
    output.frame = frame;

    UIView *descriptionView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height*0.7, frame.size.width, frame.size.height*0.3)];

    descriptionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    descriptionView.alpha = 1.0;

    UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20, 12)];
    header.textColor = [UIColor colorWithRed:210/256.0 green:217/256.0 blue:231/256.0 alpha:1.0];
    header.font = [UIFont fontWithName:@"Helvetica" size:17];
    header.backgroundColor = [UIColor clearColor];
    header.lineBreakMode = UILineBreakModeTailTruncation;
    header.numberOfLines = 1;

    UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(10, 22, frame.size.width-20, 28)];
    description.textColor = [UIColor colorWithRed:255/256.0 green:255/256.0 blue:255/256.0 alpha:1.0];
    description.font = [UIFont fontWithName:@"Helvetica" size:12];
    description.backgroundColor = [UIColor clearColor];
    description.lineBreakMode = UILineBreakModeTailTruncation;
    description.numberOfLines = 2;

    header.text = event.title;
    [descriptionView addSubview:header];

    description.text = event.shortDesription;
    [descriptionView addSubview:description];

    [output addSubview:descriptionView];

    return output;
}

After first launch everything works fine, but if I try to reload my tableView and call addImages again, all UILabels disappear, only UIImageView and UIView are visible. 首次启动后一切正常,但如果我尝试重新加载我的tableView并再次调用addImages ,则所有addImages都会消失,只有UIImageViewUIView可见。

UPD: UPD:

What I have noticed, is that my UILabels appear after some time, approximately after 30 seconds. 我注意到的是,我的UILabel在一段时间后出现,大约在30秒后出现。 I have never experienced something similar before 我以前从未经历过类似的事情

UPD 2: UPD 2:

After I reload my tableView and scrollview , scrollView's content is not updated right away, only after I start scrolling 在我重新加载tableViewscrollview之后,只有在我开始滚动之后, scrollView's内容才会立即更新

I reproduced a sample project from the code you posted, and didn't find any refreshing issues on the labels nor on any other component. 我从您发布的代码中复制了一个示例项目,并且没有在标签或任何其他组件上发现任何令人耳目一新的问题。 This leads me to think the problem is not in the code you posted, but rather somewhere else. 这让我认为问题不在你发布的代码中,而是在其他地方。

Usually when you see such a delay, it is because some code that should not be running in parallel, is running in parallel. 通常当你看到这样的延迟时,是因为一些不应该并行运行的代码并行运行。

I would suggest you check how and when you perform your call to eventsUpdated . 我建议你查看对eventsUpdated的调用方式和eventsUpdated You must make sure that call is performed on the main thread, and only once your array of events has finished updating. 您必须确保在主线程上执行调用,并且只有在您的事件数组完成更新后才会执行。 To check that, you can add add the following line in eventsUpdated : 要检查这一点,您可以在eventsUpdated添加以下行:

NSLog(@"Current: %@, main: %@", [NSThread currentThread], [NSThread mainThread]);

Please post the log! 请发布日志! :-) :-)

I would suggest you also add to your question the code from the method that calls eventsUpdated , as it could also be of help. 我建议你也在你的问题中添加调用eventsUpdated的方法中的代码,因为它也可以提供帮助。

At first, never do something like 起初,永远不要做像

 [scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

Scroll view has some internal views (scroll indicators, for example), so you're removing them which may dealloc them and cause strange crashes any time later. 滚动视图有一些内部视图(例如滚动指示符),因此您将删除它们,这可能会解除它们并在以后的任何时候导致奇怪的崩溃。

As for your problem - try calling bringSubviewToFront: of your UIImageView to get your labels infront of everything else 至于你的问题 - 尝试调用你的UIImageView的bringSubviewToFront:让你的标签面向其他一切

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM