2010-02-26 38 views
0

我從動態的API使用UISearchBar加載數據,並試圖顯示它,使用這樣的事情:EXC_BAD_ACCESS上ReloadData

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBar resignFirstResponder]; 
    NSLog(@"Search Text: %@",[searchBar text]); 
    [self startSearchingWithText:[searchBar text]]; 
} 
- (void) startSearchingWithText:(NSString *)text { 
    ... 
    // Go through the list of services and set up a query for the search term 
    QueryServiceManager *manager = [[QueryServiceManager alloc] initWithServices: services]; 

    // Set up the query 
    if (![manager addKeyword: text]) 
     NSLog(@"PROBLEM!"); 

    // Execute the query and store the titles in an array 
    self.data = [[manager makeQuery] copy]; // Returns a NSArray 

    NSLog(@"%@",self.data); 

    // Add the items to the tableView 

    [self.tableView reloadData]; 

UITableViewController代碼設置爲從self.data閱讀,這最初@""元素的NSArray,因爲這樣的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    [self.data count]; 
} 
- (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] autorelease]; 
    } 

    // Set up the cell... 

    cell.textLabel.text = [self.data objectAtIndex:indexPath.row]; 

    return cell; 
} 

我得到的,當我做到這一點:

(GDB)繼續 2010-02-26 18:43:32.515 ISAVE [11690:207]( 「東芝Satellite L505-GS5037 TruBrite 15.6英寸的筆記本電腦(黑色)」, 「戴爾靈11 11.6英寸黑曜石黑色筆記本電腦(Windows 7 Premium)「, 」華碩Eee PC貝殼1005PE-MU17-BK 10.1英寸黑色上網本 - 長達11小時的電池壽命「, 」SwissGear電腦揹包(紅色)「, 」Belkin 36- (黑色)「 」Compaq EVO N610C「, 」Belkin F8E062帶黑色錶殼(去磁工具)的55件計算機工具套件「, 」華碩Eee PC貝殼1005PE-PU17-BK 10.1 - 英國黑色上網本 - 長達14小時的電池壽命「, 」Harman Kardon SoundSticks II 2.1即插即用多媒體揚聲器系統「, 」ION音頻錄像機2 PC USB VHS視頻到電腦轉換器「 ) (gdb)繼續 編程接收信號:」EXC_BAD_ACCESS「。 (gdb)

任何想法?它看起來像NSArray被正確填充,那麼事情是失敗的ON/reloadData調用後(斷點證實這一點,但不能隔離,事情會出錯)

編輯:我更換了NSLog()與枚舉

for (NSString *elem in self.data) 
    NSLog(elem); 

而且它仍然崩潰。我設法哄日誌出來的:http://pastebin.com/NDVKLsJC

當我刪除了NSLog()完全,它不會崩潰,但UITableView不更新,要麼。

+0

你在哪裏調用reloadData? – nickthedude

+0

哦,我從來沒有見過它 – nickthedude

+0

在上面的NSLog()命令。當「搜索」按鈕被擊中時,該功能被自動調用。發佈編輯以顯示完整的定義。 –

回答

1

有些不可思議:你說你在每個部分只有1行,但是你使用indexPath.row來索引你的數組?其中之一是錯誤的,我猜這是第一個。通常,如果您在UITableView中顯示一個數組(n元素),則在該節中有一個節和n行,這意味着您將使用[myArray objectAtIndex:indexPath.row]來檢索相應的對象。

如果你想在陣列中的每個項目一個單獨的部分,然後你說你有ñ部分,並在每節1行,然後用[myArray objectAtIndex:indexPath.section]檢索合適的對象。

哦,你正在漏水到處都是。

+0

像下面的人一樣,我只是「返回1」;在那裏出於測試目的。我將修改上面的代碼,以反映,並放在正確的代碼([self.data計數]),但不管它是同樣的問題。 –

0

什麼:

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

的總是返回1代替。

+0

這不是問題;我有它返回1,這樣我可以確保它不工作在整個過程中去。如果它的工作原理1,它會更容易得到它的整個陣列工作。我剛纔編輯的代碼,以反映「正確」計數,但同樣的問題出現。 –

3

您是否確定了您嘗試訪問但已不存在的對象,該對象正在提供EXC_BAD_ACCESS?

如果沒有,那麼你應該在你的應用程序中啓用殭屍,然後你可以發出一個命令給gdb,它會告訴你哪個對象導致崩潰。

0

命中shift-command-b來分析項目並解決顯示的所有問題(潛在的泄漏等)