2010-02-08 39 views
43

其實我沒有看到我的錯誤:UITableView的數據源必須的tableView返回細胞:的cellForRowAtIndexPath:異常

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    FriendTableViewCell *cell = (FriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[FriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     [[NSBundle mainBundle] loadNibNamed:@"FriendTableViewCell" owner:self options:nil]; 
     cell = friendCell; 
    } 
    cell.lblNickname.text = @"Tester"; 
    return cell; 
} 

我到底做錯了什麼?我檢查了兩次..但沒有看到錯誤。

感謝您的幫助!

回答

66

你正在返回friendCell,它很可能是零。

您的代碼看起來不錯,因此請確保您的Interface Builder文件設置正確。在FriendTableViewCell.xib中,確保文件的所有者是您的表視圖控制器,並且您正確設置單元格爲friendCell的出口(我假設它是一個UITableViewCell)。

+1

謝謝,再次簽入後,我發現在Interface Builder中缺少插座連接! – phx 2010-02-09 17:39:26

+1

謝謝,你的回答正是我所需要的。我改變了視圖控制器類的名稱,打破了與IB的連接,而沒有考慮到它。沒有更多改變的名字,因爲我不喜歡它們! – 2010-11-23 09:35:57

+0

很好的答案,通常問題是關於返回一個零單元格! – Fattie 2013-11-05 16:14:39

2

您正在創建FriendTableViewCell,然後忽略它並將其設置爲(大概)等於名爲friendCell的實例變量。

我假設你希望在調用loadNibNamed方法時設置friendCell。它顯然沒有被設置。

所以你有這個代碼的兩個問題。首先,不要分配一個單元兩次。

cell = [[[FriendTableViewCell .... 
[[NSBundle mainBundle ..... 
cell = friendCell; 

很顯然,如果你用第二次調用來賦值給單元格,創建一個新的單元格並將其分配給單元格是無用的。

二,friendCell大概爲零。確保NIB安裝正確並且插座指向正確的位置。

+0

感謝您的幫助! friendCell是我的Outlet,那麼如何正確設置? – phx 2010-02-08 19:56:27

0

看看這裏:Loading TableViewCell from NIB

這是蘋果對這個確切的主題文件。

//This is assuming you have tvCell in your .h file as a property and IBOutlet 
//like so: 
TableViewController.h 
@property(nonatomic,retain) IBOutlet UITableViewCell *tvCell; 
//Data Source Method... 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil) { 

    [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil]; 

    cell = tvCell; 

    self.tvCell = nil; 

使用loadNibNamed:owner:options方法在nib中加載單元格。將單元格實例設置爲您的筆尖對象,然後將筆尖對象設置爲零。

閱讀我已鏈接的其他文檔,以瞭解如何在單元格內訪問子視圖。

2

我知道這是一箇舊的帖子,但是,我剛剛遇到這個錯誤,我發現它非常奇怪,因爲該應用程序正在測試,所以沒有新的構建幾天,它做到了這一點,我所做的只是重新啓動電話,它解決了它。

33

對於我來說,它的工作這樣做:

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

UPDATE
將在下面的方法上面的代碼:

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

只要把編輯tableViewCell

0

別忘了設置Interface Builder中的。

0

利用這一點,

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID]; 

我救了幾個小時,只是這行。

0

,讓您的NIB /故事板文件,確保您的重用標識符不管你叫CellIdentifier

static NSString *CellIdentifier = @"Cell"; 
1

我發現你的原型細胞的比賽,試圖初始化我的表視圖之前創建我的UITableViewCell時,這個問題就來了:

在創建tableView之前註冊類會導致錯誤,將其放置在後面將修復錯誤。

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:bCellIdentifier]; 

tableView = [[UITableView alloc] initWithFrame:CGRectZero]; 
tableView.delegate = self; 
tableView.dataSource = self; 

[self addSubview:tableView]; 

tableView.keepInsets.equal = KeepRequired(0); 
+0

在我的情況下,我調用函數來加載表UITableViewCell註冊前的數據,upvote作爲你的答案幫助我解決這個問題 – iphonemaclover 2016-08-04 10:28:54

相關問題