0

我已经在我的项目中实现了 GmGridView。但图像在 gridViewCells 中交换。这个SO POST中的答案没有帮助。代码在cellForItemAtIndex

CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
    GMGridViewCell *cell = (GMGridViewCell *)[gridView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake( 0, 0, size.width, size.height)];
        cell.reuseIdentifier = @"Cell";
        [[NSBundle mainBundle] loadNibNamed:@"HomeCustomCell" owner:self options:nil];

        [cell addSubview:_homeViewCell];
    }

    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //method to set data 
    [self configureCell:cell inGMGridView:gridView atIndexPath:index];
    return cell;

中的代码configureCell,我正在使用 `dipatch_queue' 从 url 加载图像

SNHomeCustomCell *customCell = (SNHomeCustomCell *)[cell viewWithTag:100];
    CGSize size = [self GMGridView:gmGridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
    customCell.frame = CGRectMake(0, 0, size.width, size.height);
    NSUInteger nodeCount = self.productArray.count;

    if (nodeCount > 0) {
        customCell.productImage.image = nil;
        PFObject *object = self.productArray[index];

        if (object) {
            customCell.productName.text = object[@"name"];
            customCell.productPrice.text =  [NSString stringWithFormat:@"$%@", object[@"price"]];
            dispatch_queue_t queue  = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(queue, ^{
                UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:object[@"imageUrl"]]]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [customCell.productImage setImage:image];
                });
            });

        } else {
            customCell.productName.text = @"Loading...";
            customCell.productPrice.text = @"Loading...";
        }
    }

单元格中的图像在可见单元格中向上/向下滚动时交换一次,我做错了什么?

4

1 回答 1

0

看起来dequeueReusableCellWithIdentifier:方法对您的视觉结果产生了不利影响。

滚动时,您正在重用刚刚隐藏的单元格以提供将出现的单元格。如果单元格具有动态高度,则似乎会引起一些麻烦。

我个人通过避免避免了这个麻烦dequeueReusableCellWithIdentifier:,尽管我认为这不是最好的解决方案(特别是如果您的网格包含大量单元格)。

您应该尝试更改以下内容:

GMGridViewCell *cell = (GMGridViewCell *)[gridView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
    cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake( 0, 0, size.width, size.height)];
    cell.reuseIdentifier = @"Cell";
    [[NSBundle mainBundle] loadNibNamed:@"HomeCustomCell" owner:self options:nil];

    [cell addSubview:_homeViewCell];
}

对此:

GMGridViewCell *cell = (GMGridViewCell *)[gridView dequeueReusableCellWithIdentifier:@"Cell"];
cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake( 0, 0, size.width, size.height)];

最后看看如果这个解决方案能解决你的问题,你是否能找到更好的方法。

于 2014-05-24T02:02:38.757 回答