【问题标题】:Creating Custom Cell takes the iphone app into an infinite loop创建自定义单元将 iphone 应用程序带入无限循环
【发布时间】:2009-11-13 14:37:28
【问题描述】:

我创建了一个非常简单的应用程序,我试图在表格视图中插入一个自定义单元格。但是,每当我返回自定义单元格的实例时,屏幕上什么都不会显示,其次,应用程序会进入某种奇怪的无限循环。任何帮助将非常感激。我在这个问题中附上了我的代码。

-- view controller which is returning custom cells ---

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


  // Try to recover a cell from the table view with the given identifier, this is for performance
  CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

  // If no cell is available, create a new one using the given identifier
  if (cell == nil) {
  NSArray *topLevelObjects = [[NSBundle mainBundle]
  loadNibNamed:@"sample_1ViewController" owner:self options:nil];
  for (id currentObject in topLevelObjects) 
  {
  if ([currentObject isKindOfClass:[CustomCell class]])
  {
  cell = currentObject;
  break;
  }
  }
  }

  // Fill the cell
 cell.lbl.text = @"test";
 return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return 4;
}

--- custom cell class ---

@interface CustomCell : UITableViewCell {
 IBOutlet UILabel * lbl;
}

@property (nonatomic, retain) IBOutlet UILabel * lbl;

@end

@implementation CustomCell

@synthesize lbl;

@end

----

自定义单元格是 sample_1ViewController.xib 文件中的 UITableCellView 资源。它包含一个 UILabel。 CustomCell 的标识符也是 CustomCell。

请看看你是否能找到可能有问题的地方,或者告诉我我可能遗漏的地方。

问候 尼丁

【问题讨论】:

  • 无限循环的堆栈跟踪是什么?

标签: iphone


【解决方案1】:

sample_1ViewController.nib 是否包含您的 viewController 类的实例?

每次dequeueResuableCellWithIdentifier: 返回 nil 时,您的代码似乎都会重新加载整个 nib 文件,如果该 nib 文件包含相同 viewController 类的实例,那么它将继续无限期地尝试重新加载 nib 文件。

既然你需要做的就是返回一个单元类的实例,这样怎么样:

首先,将tableCell 实例变量添加到您的 viewController 类:

@class CustomCell;
@interface MyViewController
{
    CustomCell * tableCell;
}
@end

init 方法中创建单元格的单个实例:

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self == nil) { return nil; }

    tableCell = [[CustomCell alloc] init];

    return self;
}

一定要发布到dealloc:

- (void)dealloc
{
    [tableCell release];
}

现在,您的委托方法变为:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableCell.lbl.text = @"test";
    return tableCell;
}

【讨论】:

  • 这段代码的主要问题是它假设每一行都有相同的内容:“test”。我怀疑这是对发布问题的一种简化,实际上单例单元格是不够的。
  • 非常感谢。没有尝试过,但它很有意义。所以基本上,与控制器关联的任何其他自定义视图的视图不能在同一个 xib 定义中。只是出于好奇,在这种情况下 viewController 是否也替换了视图实例。
  • @zaph:这实际上是通常使用单元格的方式。这个想法是同一个单元格在表格中的几个(如果不是全部)位置重复使用。自然,每次都必须更改有关单元的某些内容,我认为这就是 OP 打算在委托方法中执行的操作。他最初的例子只是简单地显示了添加一个“测试”标签,所以我在我的例子中做了同样的事情以避免解释混乱。
  • @unknown:我不太确定我是否理解您的问题。通常,您只需要加载 xib 文件一次(而且这通常只是通过 Interface Builder 自动发生)。每次加载 nib 时,您都会获得整个对象集的另一个副本。这可能是导致你无限循环的原因;当您第一次加载 nib 时,它会加载所有视图,并尝试为 tableView 绘制单元格,但是由于您的委托方法加载了 nib 文件的 antoher 副本,因此您得到 另一个 tableView,它再次需要绘制它的单元格,等等。
【解决方案2】:

这也是我的问题。我将单元格的视图放在 TableView 的同一个 xib 和

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

我打过电话

[[NSBundle mainBundle] loadNibNamed:CELLE_NIB owner:self options:nil];

谢谢大家!

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多