2011-02-28 52 views
0

我有一個真正的問題,發現我的問題在我的搜索控制器中。這是帶有搜索欄和搜索顯示控制器的表格視圖。它曾經工作得很好,但突然停止工作。我打開了NSZombieEnabled,它顯示我的名爲searchDataSource的NSArray是殭屍。objectAtIndex - 發送到釋放實例的消息

當您鍵入搜索詞時,「shouldReloadTableForSearchTerm」將執行handleSearchForTerm函數。 handleSearchForTerm「函數訪問我的ProductInfo類,它查詢一個SQLite數據庫並返回查詢結果,然後將這些結果放在我的searchDataSource數組中,一切看起來都很好,但是一旦我找到」cellForRowAtIndexPath「函數並嘗試加載從searchDataSource細胞,也就是當我運行中已經釋放了Array的問題

這裏是我的搜索控制器代碼:

// 
// SearchViewController.h 
// Priority Wire 
// 
// Created by Keith Yohn on 2/2/11. 
// Copyright 2011 Priority Wire & Cable. All rights reserved. 
// 
#import <UIKit/UIKit.h> 


@interface FourthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate> { 
    UITableView *mainTableView; 

    NSArray *searchDataSource; 
    NSMutableArray *contentsList; 
    NSMutableArray *searchResults; 
    NSString *savedSearchTerm; 
    NSString *webURL; 
} 

@property (nonatomic, retain) IBOutlet UITableView *mainTableView; 
@property (nonatomic, retain) IBOutlet NSArray *searchDataSource; 
@property (nonatomic, retain) NSMutableArray *contentsList; 
@property (nonatomic, retain) NSMutableArray *searchResults; 
@property (nonatomic, copy) NSString *savedSearchTerm; 
@property (nonatomic, retain) NSString *webURL; 

- (void)handleSearchForTerm:(NSString *)searchTerm; 

@end 

SearchViewController.m

// 
// SearchViewController.m 
// Priority Wire 
// 
// Created by Keith Yohn on 2/2/11. 
// Copyright 2011 Priority Wire & Cable. All rights reserved. 
// 

#import "FourthViewController.h" 
#import "ProductsDatabase.h" 
#import "ProductInfo.h" 
#import "WebViewController.h" 

@implementation FourthViewController 

@synthesize mainTableView; 
@synthesize searchDataSource; 
@synthesize contentsList; 
@synthesize searchResults; 
@synthesize savedSearchTerm; 
@synthesize webURL; 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.searchDataSource count]; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    // Set up the cell... 
    ProductInfo *info = [searchDataSource objectAtIndex:indexPath.row]; //This is where I get the 'message sent to deallocated instance' message. 
    [cell.textLabel setText:info.sName]; 
    [cell.detailTextLabel setText:info.sType]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    ProductInfo *info = [searchDataSource objectAtIndex:indexPath.row]; 

    webURL = [NSString stringWithFormat:@"http://www.prioritywire.com/specs/%@", info.sFile]; 

    WebViewController *wvController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]]; 
    wvController.URL = webURL; 
    wvController.navTitle = @"Spec Sheet"; 
    [self.navigationController pushViewController:wvController animated:YES]; 
    [wvController release]; 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 

    // Save the state of the search UI so that it can be restored if the view is re-created. 
    [self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]]; 

    [self setSearchResults:nil]; 
} 


- (void)dealloc { 
    [searchDataSource release], searchDataSource = nil; 
    [mainTableView release]; 
    [contentsList release]; 
    [searchResults release]; 
    [savedSearchTerm release]; 
    [super dealloc]; 
} 

- (void)handleSearchForTerm:(NSString *)searchTerm 
{ 
    [self setSavedSearchTerm:searchTerm]; 

    if ([self searchResults] == nil) 
    { 
     NSMutableArray *array = [[NSMutableArray alloc] init]; 
     [self setSearchResults:array]; 
     [array release], array = nil; 
    } else { 
     NSArray *productInfo = [[ProductsDatabase database] searchListing:searchTerm]; 
     self.searchDataSource = productInfo; 
     [self.mainTableView reloadData]; 
     [productInfo release]; 
    } 

    [[self searchResults] removeAllObjects]; 

    if ([[self savedSearchTerm] length] != 0) 
    { 
     for (NSString *currentString in [self contentsList]) 
     { 
      if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) 
      { 
       [[self searchResults] addObject:currentString]; 
      } 
     } 
    } 
} 

#pragma mark - 
#pragma mark UISearchDisplayController Delegate Methods 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self handleSearchForTerm:searchString]; 

    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller 
{ 
    [self setSavedSearchTerm:nil]; 
    self.searchDataSource = nil; 
    [self.mainTableView reloadData]; 
} 

@end 

我對Objective-C很陌生,無法理解我做錯了什麼。我花了好幾天的時間試圖弄明白這一點,並沒有運氣。我希望任何人都可以提供幫助。

基思

回答

1

這段代碼似乎是唯一的地方searchDataSource被設置:

NSArray *productInfo = [[ProductsDatabase database] searchListing:searchTerm]; 
    self.searchDataSource = productInfo; 
    [self.mainTableView reloadData]; 
    [productInfo release]; 

如果ProductsDatabase如下the rules,你沒有自己返回數組(即它已經自動釋放)等的釋放第四行不正確。

+0

謝謝你的回答。你是完全正確的。我一直在爲改變這門課程中的代碼做太多工作,以至於我完全放棄了它。我終於回去並刪除了所有的代碼,並重新開始,現在它正在工作。謝謝您的幫助。 – 2011-03-01 20:04:27

0

不要你的意思,而不是使用您的searchDataSourcesearchResults - 陣列,因爲在handleSearchForTerm:要添加的結果吧。爲什麼你甚至有伊瓦爾searchResult?它只用於handleSearchForTerm :,也許有一些混合?

+0

謝謝你指出。改變代碼很多時候,我不再使用searchResults數組。我昨晚決定清理所有代碼並重新開始,現在它正在工作。我設置它,以便我的ProductInfo數組現在被返回到searchResults數組,現在一切似乎都正常工作。謝謝您的幫助。 – 2011-03-01 20:06:40

相關問題