7

私はそのヘッダーとして 内部を持っUITableViewControllerています。画像のスライドショーを表示するために使用されます。UIScrollViewUIViewUIScrollView

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

すべての画像には、タイトルと説明が記載されたサブビューがあります。

UIImageView
  -UIView
    -UILabel
    -UILabel

更新する必要がある場合tableView、デリゲートメソッドが呼び出されます。このメソッドは、aにデータを再読み込みし、メソッド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が表示されなくUIImageViewなりUIView、表示されます。

UPD:

私が気付いたのは、UILabelがしばらくすると、約30秒後に表示されることです。私はこれまでに似たようなことを経験したことがありません

UPD 2:

tableViewとをリロードした後、スクロールを開始した後でのみscrollviewscrollView'sコンテンツがすぐに更新されません

4

2 に答える 2

6

投稿したコードからサンプルプロジェクトを再現しましたが、ラベルやその他のコンポーネントにさわやかな問題は見つかりませんでした。これにより、問題はあなたが投稿したコードではなく、どこかにあると思います。

通常、このような遅延が発生するのは、並列で実行されるべきではないコードが並列で実行されているためです。

に電話をかける方法とタイミングを確認することをお勧めしますeventsUpdated。イベントの配列の更新が完了した後でのみ、呼び出しがメインスレッドで実行されることを確認する必要があります。これを確認するには、次の行を追加しますeventsUpdated

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

ログを投稿してください!:-)

を呼び出すメソッドのコードも質問に追加することをお勧めしますeventsUpdated。これも役立つ可能性があるためです。

于 2012-11-29T19:00:58.910 に答える
0

最初は、次のようなことは絶対にしないでください

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

スクロールビューにはいくつかの内部ビュー(たとえば、スクロールインジケーター)があるため、それらを削除すると、それらの割り当てが解除され、後で奇妙なクラッシュが発生する可能性があります。

問題については、UIImageViewのbringSubviewToFront:を呼び出して、ラベルを他のすべての前に配置してみてください

于 2012-11-27T12:35:13.200 に答える