0

私のアプリの UI には、ページ分割された 9 つの画像のグリッドが必要です。これを行うために、UIScrollView 内にいくつかの UITables を作成しています (UIPageControl を使用):

ここに画像の説明を入力

私のテーブルは、登録したxibファイルから来ています。各テーブル セルには 3 つのボタンがあります。

ここに画像の説明を入力

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    int numPages = ([self.images count] + 8) / 9;
    NSLog(@"Page count: %i", numPages);
    // Set pages
    self.pageControl.numberOfPages = numPages;
    for (int i = 0; i < numPages; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [UIColor blackColor];
        [self.scrollView addSubview:subview];

        // Let's try and draw a table
        UITableView *table = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        table.delegate = self;
        table.dataSource = self;
        table.tag = 100 + i;
        table.separatorColor = [UIColor clearColor];
        [table setBackgroundColor:[UIColor blackColor]];
        [table registerNib:[UINib nibWithNibName:@"Cell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TestCell"];
        [self.scrollView addSubview:table];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numPages, self.scrollView.frame.size.height);
}

私の問題は、ビューの読み込みが遅い (5 秒以上) ことです。ボタンに読み込まれる画像はローカルであり、大きくはありません。

私の cellForRowAtIndexPath は次のようになります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TestCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // What table/page are we looking at?
    // Check the tag
    int page = tableView.tag - 100;
    NSLog(@"We are on page/table: %i", page);

    // Configure the cell
    // Our cells have 3 button images
    // So we'll loop and set each image
    for (int i = 0; i < 3; i++) {
        // From our images array, the item we want
        // Will be the row * 3 + i
        int imageIndex = (page * 9) + (indexPath.row * 3) + i;
        NSLog(@"Image index is: %i", imageIndex);

        // Button tags are 10-12 (or 10 + i)
        UIButton *button = (UIButton *)[cell viewWithTag:(10 + i)];
        [button addTarget:self action:@selector(poseSelected:) forControlEvents:UIControlEventTouchUpInside];

        // We have to make sure we don't go out of the bounds of
        // our images array (might not have /3 exactly)
        if (imageIndex > [self.images count] - 1) {
            // We have an extra button, let's hide it
            button.hidden = YES;
        } else {
            // Get the image and file nameefrom our array
            NSDictionary *imageDict = (NSDictionary *)[self.images objectAtIndex:imageIndex];
            NSString *fileName = [imageDict objectForKey:@"file"];
            // Make the thumb name
            NSString *thumbName = [fileName stringByReplacingOccurrencesOfString:@".jpg"
                                                                      withString:@"-THUMB.jpg"];
            // NSLog(@"Thumb name is: %@", thumbName);
            // Set the button image
            UIImage *btnImage = [UIImage imageNamed:thumbName];
            [button setImage:btnImage forState:UIControlStateNormal];
        }
    }

    return cell;
}

ログ出力を見ると、すべてのセルがすぐにセットアップされています。どうしてこれなの?

画面外のテーブル ビューをレンダリングしてはいけませんか? または、dequeueReusableCellWithIdentifier を間違った方法で使用していますか?

4

1 に答える 1

1

あなたは正しいです、あなたは cellForRowAtIndexPath とスクロールビューがどのように機能するかを誤解しています。テーブルビューはすべてスクロールビューのサブビューであるため、セルはすぐに読み込まれます。したがって、サブビューとしてのテーブルビューは、スクロールビューがビュー階層に追加されるとすぐにロードされます。cellForRowAtIndexPath は、テーブルビューをスクロールすると消えるセルのリサイクルを行います (設計上、これは理にかなっています)。私の推奨事項は、リサイクルスクロールビューを使用すること、またはカスタムグリッドビュー実装を使用することです(必要なもののようです)。私のライブラリにはいくつかの例がありますが、ニーズを満たすよりも最適なグリッド ビューまたは画像ビューアーを検索する価値があるかもしれません。私のライブラリはここにあります:

https://github.com/daltoniam/GPLib-iOS

あなたは以下を見てみたいと思うでしょう:

https://github.com/daltoniam/GPLib-iOS/tree/master/GPLib/Views/ImageViews

https://github.com/daltoniam/GPLib-iOS/tree/master/GPLib/Views/GridView

上で述べたように、私のライブラリが提供するものよりも、UI が必要とする線に沿ってグリッド ビュー/画像ビューを探す価値があるかもしれませんが、良い出発点になるはずです。ご不明な点がございましたら、お知らせください。

于 2012-08-14T05:21:27.360 に答える