2011-09-20 121 views
2

我正在使用導航控制器到達UITableView。在這個UItableView中,有一個搜索欄和50個單元格。當我不滾動,然後回來,應用程序正常行爲,但當我向下滾動像10個單元格,然後回來,我的應用程序崩潰與EXC_BAD_ACCESS錯誤。任何想法可能是這場崩潰的原因?當滾動UItableView並擊回時崩潰應用程序

在的dealloc,我釋放我在頭文件中創建的所有對象:

- (void)dealloc 
{ 

[listContent release]; 
[filteredListContent release]; 
[tmpCell release]; 
[cellNib release]; 

[super dealloc]; 
} 

和用於功能創建的細胞,它是如下:(注我做一個備用的UITableView用搜索欄)

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

static NSString *kCellID = @"cellID"; 

ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:kCellID]; 
if (cell == nil) 
{ 
    [self.cellNib instantiateWithOwner:self options:nil]; 
    cell = tmpCell; 
    self.tmpCell = nil; 
} 

/* 
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list. 
*/ 
NSDictionary *dataItem; 
if (tableView == self.searchDisplayController.searchResultsTableView) 
{ 
    dataItem = [self.filteredListContent objectAtIndex:indexPath.row]; 
} 
else 
{ 
    dataItem = [self.listContent objectAtIndex:indexPath.row]; 
} 

// Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:. 
cell.useDarkBackground = (indexPath.row % 2 == 0); 

// Configure the data for the cell. 

cell.icon = [UIImage imageNamed:@"iTaxi.jpeg"]; 
cell.publisher = [dataItem objectForKey:@"Number"]; 
cell.name = [dataItem objectForKey:@"Name"]; 
cell.price = [UIImage imageNamed:@"order-taxi.png"]; 

return cell; 
} 

ViewDidUnload具有相同的代碼作爲的dealloc

+0

你可以從後發生這種情況時控制檯全行? – Luke

+0

你可以發佈代碼嗎? – Tendulkar

+0

請發佈UIViewController中的'viewDidAppear'或'viewDidLoad'代碼,您可以從中獲得'UITableView'控制器。還有'UITableView'所在控制器的'dealloc'和'viewDidUnload'。 – Eimantas

回答

0

這是因爲,細胞被重新創建爲可見行。也就是說,當您滾動tableView時,cellForRowAtIndexPath將被調用用於可見行。在cellForRowAtIndexPath中刪除該條件if(cell==nil)。你能分享你的代碼嗎?

+0

我剛剛分享了代碼 –

+0

你有沒有試過刪除cell == nil ?? – iosfanboy9

+0

我試過了,沒有解決! –

0

該錯誤的發生是因爲地方在你的代碼你設置scrollEnabled爲「NO」(可能是當你激活搜索欄):

self.tableView.scrollEnabled = NO; 

我的意思是,如果你的searchText length是等於0(您剛纔在搜索模式下輸入),您不能禁用tableview滾動。

希望這對你有所幫助。

祝你好運,好的編碼!

法比奧Demarchi

0

當您按下回而tableview中被滾動,應用程序將獲得因爲解除了分配的實現代碼如下實例的數據源(很少委託)被稱爲協議的方法墜毀。所以我們可以在我們訪問釋放實例後得到崩潰。

爲了避免這種情況,只需在特定的視圖控制器類中添加dealloc方法,並將相應的協議設置爲nil即可。

-(void)dealloc { 

    self.yourTableView.delegate = nil; 
    self.yourTableView.dataSource = nil; 
} 

編碼快樂:)