【问题标题】:Custom UITableViewCell erroring自定义 UITableViewCell 错误
【发布时间】:2010-10-15 14:11:56
【问题描述】:

我正在尝试使用我在 IB 中构建的单元格构建自定义表格视图。我收到一个奇怪的错误:

<BroadcastViewController 0x4b4f5f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postText.

IB 中的所有内容都已正确连接到单元控制器。不太清楚为什么会这样。

这就是我的 cellForRowAtIndexPath 的样子:

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

//Get the folder object of interest
Broadcast *messageAtIndex = [self.messages objectAtIndex:indexPath.row] ;

static NSString *CellIdentifier = @"BroadcastTableViewCell";
static NSString *CellNib = @"BroadcastTableViewCell";

BroadcastTableViewCell *cell = (BroadcastTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
   //ERRORING ON THIS LINE...
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (BroadcastTableViewCell *)[nib objectAtIndex:0];
}


cell.postText.text = messageAtIndex.replyText;
cell.authorName.text = messageAtIndex.postCreatorFirstName;
cell.postDate.text = messageAtIndex.creationDate;

return cell;

}

以前有人见过这种错误吗?如果您需要更多信息,请告诉我...

【问题讨论】:

  • 需要cellForRowAtIndexPath函数的完整代码
  • 看起来您正试图在不是 NSDictionary 的东西中插入“postText”的值? :)
  • 事情是 postText 是一个字符串,它甚至没有打破那条线。我已经标记了上面导致错误的行。

标签: iphone objective-c ipad ios4


【解决方案1】:

真正奇怪的是它抱怨BroadcastViewController 类不符合postText 的KVC。

据我所知,postText 是您单元格中的一个标签,因此用于此的 IBOutlet 应该在 BroadcastTableViewCell 类中。因此,请查看您在 IB 中链接postText 标签的位置。此外,您的视图控制器中可能有一个用于此标签的 IBOutlet,您已将其删除,但您忘记删除 IB 中的链接。无论如何,有什么地方是你的问题。您在那条线上出现错误的事实只是因为您在那里加载了您的 NIB,它与单元格本身或所有者没有任何关系。

【讨论】:

  • 好吧,我已经检查并仔细检查了 IB 连接。 BroadcastTableViewCell 类中的所有内容都已正确连接。没有到任何其他视图控制器的挥之不去的连接。这真的没有任何意义......
  • 你明白了。那是笔尖里的东西。谢谢:)
【解决方案2】:

可能与返回 UITableViewCell* 的 dequeueReusableCellWithIdentifier 有关。

我通常这样做:

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier...
CustomCell* acell = (CustomCell*)cell; 

将所有者设置为 nil。

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:nil options:nil]; 

【讨论】:

  • 好的,我知道强制转换是个好主意,但是当第一个单元格为零时,这仍然对初始加载没有帮助。需要处理一些事情:“if (cell == nil)”
  • 看完之后,这几乎就是我现在所做的,只是一个更长的说法......
  • NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:nil options:nil];将所有者设置为 nil。
  • 你确定 objectAtIndex:0 确实是你的 BroadcastTableViewCell 吗?
  • 它甚至没有到达那条线。问题出在您上面描述的那一行...
【解决方案3】:

好的,想通了。 IB 中的连接确实不正确。我将它们链接到文件的所有者,而不是实际的对象。我也要给这个 Stelian 因为他指示我检查笔尖。感谢您的所有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多