【问题标题】:dequeueReusableCellWithIdentifier returns nil for iPhone. Connecting one class in two storyboardsdequeueReusableCellWithIdentifier 为 iPhone 返回 nil。连接两个故事板中的一个班级
【发布时间】:2013-04-03 09:55:37
【问题描述】:

我正在尝试为 iPhone 和 iPad 故事板使用一个类 (DetailCell)。 我有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString * cellIdentifier = @"DetailCell";
        DetailCell * detailCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (detailCell == nil) {
            detailCell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"];
        }
        //cell customization here...
    return detailCell;
}

它完美适用于 iPad,但不适用于 iPhone!所有标识符、连接和类设置正确。我还尝试擦除两个板上的单元格并创建新单元格。我实际拥有的:

在 iPad 上:

我在双端队列中获取单元格,因此它不会进入 if 并根据需要返回单元格。

在 iPhone 上:

deque 返回 nil,如果,它返回 DetailCell 但对其进行自定义不起作用。 (设置值前后所有网点均为零) 这种行为的原因是什么?

PS 在 iOS5 和 iOS6 上都

【问题讨论】:

  • dequeueReusableCellWithIdentifier:仅当具有给定标识符的任何单元格离开屏幕并排队时才返回单元格。您确定您的情况正在发生这种情况吗?
  • @nkongara in ios6 它无论如何都会返回单元格。但是在 iphone 上,它使用 init 创建单元格的次数与我的行数一样多!而且还是空桌子。

标签: iphone objective-c uitableview


【解决方案1】:

试试这个。 静态 NSString * cellIdentifier = @"DetailCell";

DetailCell * detailCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (detailCell == nil)
{
    detailCell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"];

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YourNibName" owner:self options:nil];
    for (id oneObject in nib) if ([oneObject isKindOfClass:[DetailCell class]])
        cell = (DetailCell *)oneObject;
}
//cell customization here...
return detailCell;

【讨论】:

  • 您在 if 块内创建 2 个单元格。第一个与 OP 有相同的问题,第二个只是无缘无故立即被丢弃:/。如果您更正第二个以正确分配给detailCell 而不是cell,那么您仍然无缘无故地通过alloc/init* 创建了一个单元格
  • 抱歉,这段代码没有意义……更多的是我使用故事板
猜你喜欢
  • 2012-04-17
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 2015-03-06
  • 1970-01-01
相关资源
最近更新 更多