Loading a Reusable UITableViewCell from a Nib

前端 未结 16 2220
遇见更好的自我
遇见更好的自我 2020-11-27 09:59

I am able to design custom UITableViewCells and load them just fine using the technique described in the thread found at http://forums.macrumors.com/showthread.php?t=545061.

16条回答
  •  感情败类
    2020-11-27 10:17

    This technique also works and doesn't require a funky ivar in your view controller for memory management. Here, the custom table view cell lives in a xib named "CustomCell.xib".

     static NSData *sLoadedCustomCell = nil;
    
     cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
     if (cell == nil) 
     {
       if (sLoadedCustomCell == nil) 
       {        
          // Load the custom table cell xib
          // and extract a reference to the cell object returned
          // and cache it in a static to avoid reloading the nib again.
    
          for (id loadedObject in [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]) 
          {
            if ([loadedObject isKindOfClass:[UITableViewCell class]]) 
            {
              sLoadedCustomCell = [[NSKeyedArchiver archivedDataWithRootObject: loadedObject] retain];
              break;
            }
        }
        cell = (UITableViewCell *)[NSKeyedUnarchiver unarchiveObjectWithData: sLoadedCustomCell];
      }
    

提交回复
热议问题