2012-04-03 83 views
-1

我有一個問題,而裝上的UITabBarController一個UITableView,我同時使用裝表標籤在基於標籤的iPhone應用程序中加載UITableView?

終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由得到這個錯誤:「UITableView的數據源必須返回從的tableView細胞:的cellForRowAtIndexPath: 「

我的代碼是在這裏

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

// Return the number of sections. 
return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

// Return the number of rows in the section. 
return 2; 
} 

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

// Configure the cell... 

return cell; 
} 

我嘗試了很多,這可能是簡單的..任何人都幫我嗎?

回答

1
//Editing in your code, it should work. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return 2; 
} 

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

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

    return cell; 
} 
+0

當你不需要創建一個新的'cell'時,你忘記檢查'if(cell == nil)'的條件,那麼它將被重用。 – Hemang 2012-04-03 11:17:29

+0

感謝您的重播,是的,我忘記檢查條件。謝謝,現在它工作正常。 – 2012-04-03 11:44:27

+0

@NeerajNeeru,gr8 happie編碼!:) – Hemang 2012-06-19 07:26:00

0

問題是您正在使用dequeueReusableCellWithIdentifier,但如果返回nil(即,尚未創建要重新使用的單元格),則無法處理該代碼。

例如:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
+0

感謝您的重播,我知道了。 – 2012-04-03 11:42:53

相關問題