Loading a Reusable UITableViewCell from a Nib

前端 未结 16 2128
遇见更好的自我
遇见更好的自我 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:12

    Actually, since you are building the cell in Interface Builder, just set the reuse identifier there:

    Or if you are running Xcode 4, check the Attributes inspector tab:

    enter image description here

    (Edit: After your XIB is generated by XCode, it contains an empty UIView, but we need a UITableViewCell; so you have to manually remove the UIView and insert a Table View Cell. Of course, IB will not show any UITableViewCell parameters for a UIView.)

    0 讨论(0)
  • 2020-11-27 10:12

    From the UITableView docs regarding dequeueWithReuseIdentifier: "A string identifying the cell object to be reused. By default, a reusable cell’s identifier is its class name, but you can change it to any arbitrary value."

    Overriding -reuseIdentifer yourself is risky. What happens if you have two subclasses of your cell subclass, and use both of these in a single table view? If they send the reuse identifier call onto super you'll dequeue a cell of the wrong type.............. I think you need to override the reuseIdentifier method, but have it return a supplanted identifier string. Or, if one has not been specified, have it return the class as a string.

    0 讨论(0)
  • 2020-11-27 10:15

    Just implement a method with the appropriate method signature:

    - (NSString *) reuseIdentifier {
      return @"myIdentifier";
    }
    
    0 讨论(0)
  • 2020-11-27 10:15

    Here is another option:

    NSString * cellId = @"reuseCell";  
    //...
    NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];
    
    for (id obj in nibObjects)
    {
        if ([obj isKindOfClass:[CustomTableCell class]])
        {
            cell = obj;
            [cell setValue:cellId forKey:@"reuseIdentifier"];
            break;
        }
    }
    
    0 讨论(0)
  • 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];
      }
    
    0 讨论(0)
  • 2020-11-27 10:20

    For what it's worth, I asked an iPhone engineer about this at one of the iPhone Tech Talks. His answer was, "Yes, it's possible to use IB to create cells. But don't. Please, don't."

    0 讨论(0)
提交回复
热议问题