7

我有UITableViewControllerinsideUIScrollView作为UIView它的标题。 UIScrollView用于显示图像的幻灯片:

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

每个图像都有一个带有标题和描述的子视图:

UIImageView
  -UIView
    -UILabel
    -UILabel

当我需要更新我tableView的委托方法时,它会在 atableView和调用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 都会消失,只有UIImageView并且UIView可见。

升级版:

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

更新 2:

重新加载tableViewand后scrollviewscrollView's内容不会立即更新,只有在我开始滚动之后

4

2 回答 2

6

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

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

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

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

请发布日志!:-)

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

于 2012-11-29T19:00:58.910 回答
0

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

于 2012-11-27T12:35:13.200 回答