【问题标题】:the class is not key value coding-compliant for the key tableView该类与键 tableView 的键值编码不兼容
【发布时间】:2017-06-21 13:10:10
【问题描述】:

在 StoryBoard 中,我有一个场景包含一些 TextField、按钮和 UITableView。在头文件中声明了 UITableView 的一个实例,但是当我尝试访问方法“cellForRowAtIndexPath”时,我收到以下错误

this class is not key value coding-compliant for the key tableView.

我用谷歌搜索了那个错误,一些结果表明它是关于将 DataSource 和 Delegate 属性链接到 ViewController,但我仍然收到同样的错误

请告诉我如何解决此错误。

code-1

 -(UITableViewCell *) tableView:(UITableView *)tableView    
 cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  NSLog(@"cellForRowAtIndexPath");
   NSString *cellIdentifier = @"cellId";

   UITableViewCell *cell = [tableView1    
   dequeueReusableCellWithIdentifier:cellIdentifier 
   forIndexPath:indexPath];

    if (cell == nil) {
    cell = [[UITableViewCell alloc] 
   initWithStyle:UITableViewCellStyleDefault 
   reuseIdentifier:cellIdentifier];
   }

   //[cell.textLabel setText: [dataArray 
   objectAtIndex:indexPath.row]];

  if (textFieldRowNameAsString != nil)
    cell.textLabel.text = textFieldRowNameAsString;

 return cell;

 }

代码 2

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate,   
UITableViewDataSource>
{
NSString *textFieldRowNameAsString;
UITableView *tableView1;
UITableViewCell *prototypeCell;
UIView *contentView;

}

image-1

【问题讨论】:

  • 您想要“访问方法'cellForRowAtIndexPath'”的原因是什么?这不是你通常需要做的。

标签: ios objective-c uitableview


【解决方案1】:

如果你这样做,你可以避免错误

在所有表视图数据源和委托方法中始终使用传递的tableView 实例

而且您不需要检查单元格中的nil,因为这个dequeueReusable 方法总是返回一个有效的单元格。

由于单元格被重复使用,强烈建议将所有 UI 元素始终设置为已定义的状态。

-(UITableViewCell *) tableView:(UITableView *)tableView    
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSString *cellIdentifier = @"cellId";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

      if (textFieldRowNameAsString != nil) {
         cell.textLabel.text = textFieldRowNameAsString;
      } else {
         cell.textLabel.text = ""
      }

     return cell;
 }

PS:如果你想拥有一个自定义的表视图变量/属性,你必须创建一个IBOutlet 并将表视图连接到它。一个简单的属性是不够的。

【讨论】:

  • 谢谢..请您查看我在上面发布的图片...请在右侧部分有很多,在“参考网点”中有
【解决方案2】:

按照以下步骤操作:这可能会解决您的问题。

删除storyboard中tableView1的连接,重新连接正确的变量名试试。仔细检查标识符字符串文本。它应该完全匹配。

【讨论】:

  • 谢谢。我拥有的 uitableView 对象称为 tableView1 ..当我检查视图控制器和 tableView 之间的连接以设置数据源和委托时,我发现它被称为 tableView “我的意思是引用插座”如图所示发布在上面的更新部分..有没有办法将名称从 tableView 更改为 tableView1
  • 是的,您可以更改。首先删除与 tableView 的现有连接,然后将 tableView1 连接到 storyboard 中的 tableView。这样您就可以将名称从 tableView 更改为 tableView1。
【解决方案3】:

请务必在选择视图组件后从 Storyboard 中选中“从目标继承模块”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2013-05-23
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多