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

标签 iphone objective-c ios cocoa-touch uiview

我有 UITableViewControllerUIView 里面的 UIScrollView 作为它的标题。 UIScrollView 用于显示图片幻灯片:

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

每张图片都有一个带有标题和描述的 subview :

UIImageView
  -UIView
    -UILabel
    -UILabel

当我需要更新我的 tableView 时,会调用一个委托(delegate)方法,该方法会在 tableView 中重新加载数据并调用 addImages 方法:

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

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

这是我添加图片的方式:

- (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;
}

首次启动后一切正常,但如果我尝试重新加载我的 tableView 并再次调用 addImages,所有 UILabel 都会消失,只有 UIImageViewUIView 是可见的。

更新:

我注意到,我的 UILabel 会在一段时间后出现,大约 30 秒后。我以前从未经历过类似的事情

更新 2:

在我重新加载我的 tableViewscrollview 后,scrollView 的 内容不会立即更新,只有在我开始滚动后才会更新

最佳答案

我从您发布的代码中复制了一个示例项目,并没有在标签或任何其他组件上发现任何令人耳目一新的问题。这让我认为问题不在您发布的代码中,而是在其他地方。

通常当你看到这样的延迟时,是因为某些不应该并行运行的代码正在并行运行。

我建议您检查如何以及何时执行对 eventsUpdated 的调用。您必须确保调用是在主线程上执行的,并且只有在您的事件数组完成更新后才执行。要检查这一点,您可以在 eventsUpdated 中添加以下行:

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

请贴出日志! :-)

我建议您还将调用 eventsUpdated 的方法中的代码添加到您的问题中,因为它也可能有所帮助。

关于iphone - iOS - UIScrollView 不更新其内容,一些 UIViews 在重新加载后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573028/

相关文章:

iphone - 如何在 iOS 上将文件存储在不同的临时/缓存文件夹中

ios - iOS 中通用链接和旧 URL 方案之间的区别

objective-c - 无需用户点击 UITextView 即可调用 becomeFirstResponder

objective-c - 您可以将 KVO 与 Scripting Bridge 一起使用吗

ios - 如何仅启动具有手机号码的联系人的 ABPeoplePickerNavigationController?

ios - UITableViewCell 内的 UICollectionView

ios - 无法运行 'Pod Setup'

iphone - 图像创建函数中的内存泄漏

ios - 使用transitionStyle .scroll时如何将UIPageViewControllerSpineLocation设置为.mid

ios - iphone 的电子邮件验证