2012-08-17 33 views
0

UITableViewController崩潰時出現壞的訪問錯誤。這是我的代碼:滾動tableviewcontroller時出現錯誤

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    NSString *cellText ; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
    } 
    if (indexPath.row == 0) 
    { 
     cellText = @"Mail1"; 
    } 
    else if (indexPath.row == 1) 
    { 
     cellText = @"Mail2"; 
    } 
    else if (indexPath.row == 2) 
    { 
     cellText = @"Mail3"; 
    } 
    else if(indexPath.row == 3) 
    { 
     cellText = @"Mail4"; 
    } 
    else 
    { 
     cellText = @"Mail5"; 
    } 
    cell.textLabel.text = cellText; 
    return cell; 
} 

這是日誌消息的Xcode給

XXXXProject [1366:F803] * - [XXXXViewController 的tableView:的cellForRowAtIndexPath:]:發送消息釋放 實例0x6a793f0

+3

你可以發佈崩潰日誌嗎? – janusbalatbat 2012-08-17 09:10:49

+2

哪一行應用程序崩潰? – Vladimir 2012-08-17 09:11:02

+0

XXXXProject [1366:f803] *** - [XXXXViewController tableView:cellForRowAtIndexPath:]:發送到釋放實例0x6a793f0的消息這是日誌消息xcode給了@janusfidel – ohmprakash 2012-08-17 09:13:00

回答

0

我認爲你不必初始化單元格。也許storyboard初始化單元格。

ss

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

    // Configure the cell... 
    NSString *cellText; 
    if (indexPath.row == 0) 
    { 
     cellText = @"Mail1"; 
    } 
    else if (indexPath.row == 1) 
    { 
     cellText = @"Mail2"; 
    } 
    else if (indexPath.row == 2) 
    { 
     cellText = @"Mail3"; 
    } 
    else if(indexPath.row == 3) 
    { 
     cellText = @"Mail4"; 
    } 
    else 
    { 
     cellText = @"Mail5"; 
    } 
    cell.textLabel.text = cellText; 
    return cell; 
} 

您可以從GitHub下載此示例項目,只要運行它。

https://github.com/weed/p120817_TableTest

+0

這是我得到的錯誤 XXXXProject [1906:f803] ***聲明失敗 - [UITableView _createPreparedCellForGlobalRow:withIndexPath:],/SourceCache/UIKit_Sim/UIKit-1914.84/UableViewView.m:6061 2012-08-17 15 :23:02.965 NewProject [1906:f803] ***終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'UITableView dataSource必須返回從 – ohmprakash 2012-08-17 09:53:07

+0

的單元格,該網址根本不工作。 – ohmprakash 2012-08-17 09:59:53

+0

對不起! http://github.com/weed/p120817_TableTest – weed 2012-08-17 10:13:27

0

完全相同的問題,只是貼在StackOverflow的前幾個小時。 (沒有一個表視圖叫它的委託,而是一個UIButton它的動作)。問題可能是你沒有強大的引用你的表視圖控制器,因此它在創建後立即被釋放。 See my answer to the previous question.

+0

plz詳解@ H2CO3 – ohmprakash 2012-08-21 03:02:48