0

我的应用程序的 UI 需要分页的 9 个图像的网格。为此,我在 UIScrollView(带有 UIPageControl)中创建了几个 UITable:

在此处输入图像描述

我的表来自我注册的 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 对滚动表格视图时消失的单元格进行回收(按照设计,这是有道理的)。我的建议是使用回收滚动视图,甚至更好的自定义 gridview 实现(似乎是你需要的)。我的库中有一些示例,但可能值得搜索最好的网格视图或图像查看器,而不是满足您的需求。我的图书馆可以在这里找到:

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 回答