0

I have implemented GmGridView in my project. but images are swapping in gridViewCells. the answer in this SO POST didn't helped. Code in 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;

Code in configureCell, I'm using `dipatch_queue' to load images from urls

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

the image's in cells are swapping once in scroll up/down in the visible cell, what am I doing wrong?

Community
  • 1
  • 1
Rugmangathan
  • 3,186
  • 6
  • 33
  • 44

1 Answers1

0

It looks like dequeueReusableCellWithIdentifier: method is adversely affecting your visual result.

While scrolling, you are reusing the cells which have just been hidden in order to provide the cells which will appear. If the cells have dynamic height, it seems to cause some trouble.

I personally saved this trouble by avoiding dequeueReusableCellWithIdentifier:, although I don't think it is the best solution (especially if your grid contain a large number of cells).

You should try to change the following:

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];
}

to this:

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

and finally see if you can find a better way to proceed if this solution is solving your trouble.

sebastien FCT
  • 630
  • 1
  • 13
  • 28