2017-06-21 118 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

相關問題