2012-03-24 48 views
2

我注意到每次我的GMGridView需要刷新時,它會重新創建所有單元,這需要很長時間。iOS GMGridView如何重用GMGridViewCell單元格?

是否有某種方法可以將重用標識符分配給GMGridViewCell或以某種方式確保它們可重用?

下面是我每次重新創建所有可見視圖的代碼。

- (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; 
    } 
+0

我已經使用分配

同一樣品代碼在我的應用程序中,我有76個視圖,它只創建了31個視圖,而不是每次都創建一個新視圖。 – Leena 2012-03-24 08:33:52

回答

0

如果您的代碼被重新使用,您的代碼似乎沒有做任何事情。這篇文章應該指出你在正確的方向... GitHub

0

可能是爲時已晚爲回答這個問題,但我有這個答案。

重用GMGridCell,你需要指定重用標識符你的Alloc後您的電池 可能是這樣

cell.reuseIdentifier = [NSString stringWithFormat:@"Cell%i", index]; 
0

對不起,遲到的答覆。

我認爲對於sigle/group tableview上的可重用單元,只需在聲明和分配nil單元格時添加此代碼即可。

代碼 - :

//聲明

//如果(細胞==無) {

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

}