2

I noticed that every time my GMGridView needs to refresh, it re-creates all cells, which takes a long time.

Is there some way to assign a reuse identifier to GMGridViewCell or somehow ensure they are reusable?

Here's the code that I have that re-creates all visible views each time.

  - (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
    {
        NSLog(@"Creating view indx %d", index);

        CGSize size = [self sizeForItemsInGMGridView:gridView];

        GMGridViewCell *cell = [gridView dequeueReusableCell];

        if (!cell) 
        {
            cell = [[GMGridViewCell alloc] init];
            cell.deleteButtonIcon = [UIImage imageNamed:@"close_x.png"];
            cell.deleteButtonOffset = CGPointMake(30, -20);

            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 57, 57)];

            cell.userData = [[IconFile allObjects] objectAtIndex:index];

            UIImageView* imageView = [[UIImageView alloc] initWithFrame:view.frame];
            NSIndexPath* indexPath = [NSIndexPath indexPathForRow:index inSection:0];
            IconFile* iconFile_ = [self.fetchedResultsController objectAtIndexPath:indexPath];

    //        imageView.image = [UIImage imageNamed:@"retina_114x114_1.png"];
            imageView.image = [UIImage imageWithData:iconFile_.image114];
            [view addSubview:imageView];
            imageView.center = view.center;
            imageView.layer.masksToBounds = YES;
            imageView.layer.cornerRadius = 9;

            view.backgroundColor = [UIColor clearColor];
    //        view.layer.masksToBounds = YES;
    //        view.layer.cornerRadius = 9;
            view.layer.shadowColor = [UIColor grayColor].CGColor;
            view.layer.shadowOffset = CGSizeMake(5, 5);
            view.layer.shadowPath = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
            view.layer.shadowRadius = 9;

            ShadowLabel *label = [[ShadowLabel alloc] initWithFrame:CGRectMake(0,0,72,21)];
            label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //        label.text = (NSString *)[_data objectAtIndex:index];
            label.text = iconFile.springBoardName;

            label.layer.shadowPath = [UIBezierPath bezierPathWithRect:label.bounds].CGPath;
            label.layer.shadowRadius = 9;        
            label.textAlignment = UITextAlignmentCenter;
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont boldSystemFontOfSize:11];
            [view addSubview:label];
            label.center = CGPointMake(size.width/2, 60);


            cell.contentView = view;
        }else{

    //        [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //        
    //        UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
    //        label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //        label.text = (NSString *)[_data objectAtIndex:index];
    //        label.textAlignment = UITextAlignmentCenter;
    //        label.backgroundColor = [UIColor clearColor];
    //        label.textColor = [UIColor blackColor];
    //        label.font = [UIFont boldSystemFontOfSize:20];
    //        [cell.contentView addSubview:label];
        }
        return cell;
    }
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • i have used the same sample code in my app and i have 76 views and it is creating only 31 views it is not creating a new view every time. – Leena Mar 24 '12 at 08:33

3 Answers3

0

Your code doesn't seem to do anything to the cell if its being re-used. This post should point you in the right direction...GitHub

ChickensDontClap
  • 1,871
  • 4
  • 22
  • 39
0

Might be this is too late to answer this Question but I have an answer for this.

For Reusing GMGridCell, you need to assign reuse identifier after you alloc your cell May be like this

    cell.reuseIdentifier =  [NSString stringWithFormat:@"Cell%i", index];
DivineDesert
  • 6,924
  • 1
  • 29
  • 61
0

Sorry for late reply.

i think for reusable cell on sigle/group tableview just add this code on declaring and allocation on nil cell.

Code -:

//Declaring

 UItableViewCell *cell = [tbl_List dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row]];

// allocating

if (cell == nil) {

     cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row]];

 }
Anjan
  • 360
  • 1
  • 5
  • 20