2017-06-21 115 views
0

在StoryBoard中我有一个场景包含一些TextFields,按钮和一个UITableView。在UITableView的实例是在头文件中声明,但是当我试图访问该方法“的cellForRowAtIndexPath”我收到以下错误该类不是关键值编码兼容的关键tableView

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

我GOOGLE了这个错误,有的结果表明,它是关于链接DataSource和委托属性的ViewController,但我仍然收到相同的错误

请让我知道如何解决这个错误。

代码-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; 

} 

图像-1

enter image description here

+0

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

回答

1

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

删除故事板中tableView1的连接并重新连接到正确的变量名并尝试。仔细检查标识符字符串文本它应该完全相同。

+0

谢谢。我有的uitableView对象被称为tableView1..and当我检查视图控制器和tableView之间的连接设置数据源和委托,我发现它被称为tableView“我的意思是参考插座”如图所示张贴在上面的更新部分..有没有办法将名称从tableView更改为tableView1 – user2121

+0

是的,你可以改变。首先删除现有的连接到tableView,然后将tableView1连接到storyboard中的tableView。这样您可以将名称从tableView更改为tableView1。 –

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和表视图连接到它。简单的财产是不够的。

+0

谢谢..请您检查我张贴在上面的图片...请在右边的部分有很多,在“参考网点”有 – user2121

+0

确定,但是出口在代码中不可见。 – vadian

相关问题