1

我在 GridView 中有 45 个项目(12 行)的 AQGridView 视图,当我滚动视图时出现问题,并且在创建新行时网格变得滞后。我正在考虑创建一次整行并从缓存或其他东西中使用它,而不是每次滚动时都要求创建它,因为它不可用并且滞后时看起来不太好。谢谢

- (AQGridViewCell *) gridView: (AQGridView *) aGridView cellForItemAtIndex: (NSUInteger) `enter code here`index
{
    NSString *fullThumbPath = [itemsList objectAtIndex:index];
    int startOfThumbWord = [fullThumbPath rangeOfString:@"bundle"].location;
    NSString *shortThumbPath = [fullThumbPath substringFromIndex:startOfThumbWord+7];
    if (
        ([vieta isEqualToString:@"cover"] || [vieta isEqualToString:@""]) && [labelis isEqualToString:@""]) {
        static NSString * PlainCellIdentifier = @"ImageCell";
        AFInstallerImageCell2 * plainCell2 = (AFInstallerImageCell2 *)[aGridView dequeueReusableCellWithIdentifier: PlainCellIdentifier];
            plainCell2 = [[AFInstallerImageCell2 alloc] initWithFrame: CGRectMake(0.0, 0.0, 200.0, 150.0) // 330
                                                    reuseIdentifier: PlainCellIdentifier];

    plainCell2.selectionStyle = AQGridViewCellSelectionStyleNone;
    plainCell2.path = [target stringByAppendingPathComponent:shortThumbPath];//[itemsList objectAtIndex:index];
    plainCell2.image = [UIImage imageWithContentsOfFile:[itemsList objectAtIndex:index]];
    plainCell2.layer.shouldRasterize = YES;
    NSString *shortThumbPath = [fullThumbPath substringFromIndex:startOfThumbWord+30];

    shortThumbPath = [shortThumbPath stringByReplacingOccurrencesOfString:@"/Thumb.png"
                                         withString:@""];

    NSString *title = [[shortThumbPath lastPathComponent] stringByDeletingPathExtension];
    plainCell2.title = [title stringByReplacingOccurrencesOfString:@"_" withString:@" "];
    return ( plainCell2 );
}
4

1 回答 1

0

这是因为您一次又一次地创建plainCell2

对 tableView/gridview 使用 dequeueReusableCellWithIdentifier,可以大大加快速度。无需实例化大量单元格,您只需实例化所需数量的单元格,即尽可能多的可见单元格(这是自动处理的)。但是您每次都在创建一个新单元格

替换这个

 AFInstallerImageCell2 * plainCell2 = (AFInstallerImageCell2 *)[aGridView dequeueReusableCellWithIdentifier: PlainCellIdentifier];
            plainCell2 = [[AFInstallerImageCell2 alloc] initWithFrame: CGRectMake(0.0, 0.0, 200.0, 150.0) // 330
                                                    reuseIdentifier: PlainCellIdentifier];

有了这个

     AFInstallerImageCell2 * plainCell2 = (AFInstallerImageCell2 *)[aGridView dequeueReusableCellWithIdentifier: PlainCellIdentifier];
              if(plainCell2 ==nil) { 
plainCell2 = [[AFInstallerImageCell2 alloc] initWithFrame: CGRectMake(0.0, 0.0, 200.0, 150.0) // 330
                                                        reuseIdentifier: PlainCellIdentifier];
}
于 2013-08-27T11:14:31.137 回答