Iphone 在UIScrollView中对多个表使用dequeueReusableCellWithIdentifier

Iphone 在UIScrollView中对多个表使用dequeueReusableCellWithIdentifier,iphone,objective-c,cocoa-touch,uitableview,Iphone,Objective C,Cocoa Touch,Uitableview,我的应用程序的UI需要由9个分页图像组成的网格。 为此,我在UIScrollView中创建了几个UITables(带有UIPageControl): 我的表格来自我注册的一个xib文件。每个表格单元格中有3个按钮 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. int numPages = ([self.images count]

我的应用程序的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);
}

-(void)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
整数=([self.images count]+8)/9;
NSLog(@“页面计数:%i”,numPages);
//设置页面
self.pageControl.numberOfPages=numPages;
对于(int i=0;i
我的问题是视图加载缓慢(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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“TestCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//我们在看什么表格/页面?
//检查标签
int page=tableView.tag-100;
NSLog(@“我们在第页/表格:%i”,第页);
//配置单元格
//我们的细胞有3个按钮图像
//因此,我们将循环并设置每个图像
对于(int i=0;i<3;i++){
//从我们的图像阵列中,我们想要的项目
//将是第*3+1行
int imageIndex=(第9页)+(indexath.row*3)+i;
NSLog(@“图像索引为:%i”,图像索引);
//按钮标签为10-12(或10+i)
UIButton*按钮=(UIButton*)[带标记的单元格视图:(10+i)];
[按钮添加目标:自我操作:@选择器(位置选择:)用于控制事件:UIControlEventTouchUpInside];
//我们必须确保我们不会越界
//我们的图像阵列(可能没有/3)
如果(imageIndex>[self.images count]-1){
//我们有一个额外的按钮,我们把它藏起来吧
button.hidden=是;
}否则{
//从阵列中获取图像和文件名
NSDictionary*imageDict=(NSDictionary*)[self.images对象索引:imageIndex];
NSString*文件名=[imageDict objectForKey:@“文件”];
//取拇指的名字
NSString*thumbName=[fileName stringByReplacingOccurrencesOfString:@.jpg”
带字符串:@“-THUMB.jpg”];
//NSLog(@“拇指名称为:%@”,拇指名称);
//设置按钮图像
UIImage*btnImage=[UIImage ImageName:thumbName];
[按钮设置图像:btnImage for状态:uicontrol状态正常];
}
}
返回单元;
}
当我查看日志输出时,所有单元格都立即得到设置。为什么会这样?


不应该渲染屏幕外的表视图吗?还是我使用dequeueReusableCellWithIdentifier的方式不对

您是对的,您误解了cellForRowAtIndexPath和滚动视图的工作原理。您的单元格将立即加载,因为您的表视图都是scrollview中的子视图。因此,当scrollview添加到视图层次结构时,作为子视图的TableView将立即加载。cellForRowAtIndexPath对滚动表格视图时消失的单元格进行循环(按设计,这是有意义的)。我的建议是使用scrollview,或者更好的是使用自定义gridview实现(这似乎是您所需要的)。我的库中有一些示例,但可能值得搜索满足您需求的最佳网格视图或图像查看器。我的图书馆可以在这里找到:

您将要查看:


正如我上面所说的,可能值得寻找一个网格视图/图像视图,它更符合您的UI需要,而不是我的lib提供的内容,但这应该是一个很好的起点。任何问题都让我知道

[UIImage ImageName:thumbName]
是同步的(它会暂停主应用程序线程,直到加载图像为止)。您应该使用一些库,例如允许流体加载占位符图像。此外,您可以保留图像的缩略图以更快地加载它们。我正在加载相当小的缩略图。SDWebImage会加载本地文件吗?它使用
NSURL
,所以我想你可以给他一个本地文件路径。试试看;)