我能够设计自定义UITableViewCells并使用http://forums.macrumors.com/showthread.php?t=545061上的线程中介绍的技术很好地加载它们。但是,使用该方法不再允许您使用redirectIdentifier初始化单元格,这意味着您必须在每次调用时为每个单元格创建全新的实例。有没有人找到一种仍然可以缓存特定单元格类型以供重用,但是仍然能够在Interface Builder中进行设计的好方法?
我能够设计自定义UITableViewCells并使用http://forums.macrumors.com/showthread.php?t=545061上的线程中介绍的技术很好地加载它们。但是,使用该方法不再允许您使用redirectIdentifier初始化单元格,这意味着您必须在每次调用时为每个单元格创建全新的实例。有没有人找到一种仍然可以缓存特定单元格类型以供重用,但是仍然能够在Interface Builder中进行设计的好方法?
Answers:
只需使用适当的方法签名来实现一个方法:
- (NSString *) reuseIdentifier {
  return @"myIdentifier";
}return NSStringFromClass([self class]);
                    现在,在iOS 5中,有一个适合的UITableView方法:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier我不记得最初在哪里找到此代码,但是到目前为止,它对我来说一直很好。
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomTableCell";
    static NSString *CellNib = @"CustomTableCellView";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }
    // perform additional custom work...
    return cell;
}接口构建器设置示例...
看看我对这个问题的回答:
是否可以在Interface Builder中设计NSCell子类?
不仅可以在IB中设计UITableViewCell,而且还很理想,因为否则所有手动布线和多个元素的放置都非常繁琐。只要可以使所有元素不透明,性能就可以。在IB中为UITableViewCell的属性设置了复用ID,然后在尝试出队时在代码中使用匹配的复用ID。
去年在WWDC上的一些演讲者中,我还听说您不应该在IB中创建表格视图单元格,但这是一大堆东西。
从iOS约4.0开始,iOS文档中有一些特定说明使此工作超快速:
向下滚动到有关子类化UITableViewCell的地方。
这是另一个选择:
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;
    }
}UITableViewCell设置的唯一值reuseIdentifer。我认为这是原始操作人员真正想要的。
                    我以类似的方式创建自定义视图单元格-除了通过IBOutlet连接单元格外。
该[nib objectAt...]方法容易改变阵列中项目的位置。
这种UIViewController方法很好-只是尝试了一下,效果很好。
但...
在所有情况下,initWithStyle都不会调用构造函数,因此不会进行默认初始化。
我已经阅读过许多有关使用initWithCoder或的地方awakeFromNib,但没有确凿的证据表明这两种方法都是正确的。
除了在方法中显式调用某些初始化方法外,cellForRowAtIndexPath我还没有找到答案。
不久前,我在blog.atebits.com上找到了一篇有关该主题的出色博客文章,此后开始使用Loren Brichter ABTableViewCell类来处理我的所有UITableViewCells。
您最终得到了一个简单的容器UIView来放置所有小部件,并且滚动速度很快。
希望这是有用的。
此技术也可以使用,并且不需要在视图控制器中使用时髦的ivar进行内存管理。在这里,定制表视图单元格位于名为“ CustomCell.xib”的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];
  }路易法对我有用。这是我用来从笔尖创建UITableViewCell的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];
    if (cell == nil) 
    {
        UIViewController *c = [[UIViewController alloc] initWithNibName:@"CustomCell" bundle:nil];
        cell = (PostCell *)c.view;
        [c release];
    }
    return cell;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}         
return cell;
}gustavogb解决方案对我不起作用,我尝试过的是:
ChainesController *c = [[ChainesController alloc] initWithNibName:@"ChainesController" bundle:nil];
[[NSBundle mainBundle] loadNibNamed:@"ChaineArticleCell" owner:c options:nil];
cell = [c.blogTableViewCell retain];
[c release];它似乎有效。blogTableViewCell是该单元的IBOutlet,而ChainesController是该文件的所有者。
对于它的价值,我在一次iPhone技术座谈会上询问了一位iPhone工程师。他的回答是:“是的,可以使用IB创建单元格。但是请不要,请不要。”