GMGridViewCells中的图像(使用xib)在滚动时进行交换

时间:2014-05-23 07:04:01

标签: ios objective-c gmgridview gmgridviewcell

我在我的项目中实现了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'从网址加载图片

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...";
        }
    }

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

1 个答案:

答案 0 :(得分: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)];

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