2012-10-08 40 views
0

我想使用tableview + searchDisplay與兩個不同的來源,也就是說,當用戶嘗試搜索searchbar時,它應該從不同的數組搜索並顯示它。ios tableview儘快搜索開始

我能做到這一點的方法是,在表視圖委託中區分源,並在searchdisplay調用表視圖委託方法時處理不同的源。所以,我不喜歡的東西:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(self.tableView == tableView){ 
    return [self.sourceArray objectAtIndex:section] count]; 
    } 
    //search display 
    return 5; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.tableView == tableView ? 3 : 1; 
} 

但一旦我試圖在搜索欄搜索(只要文本發生變化),它只是 索引越界墜毀。我試圖調試它,但無法找到真正的錯誤。

2012-10-08 14:36:27.075 Apps[14496:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]' 
*** First throw call stack: 
(0x37b9b2a3 0x35eab97f 0x37ae5e8d 0x391f6221 0x38ea1fef 0x38e9cd09 0x38eb58f9 0x38eb54f9 0x2934773 0x38fd2ac5 0x38fca943 0x38fca8ff 0x38f5c48f 0x38f94235 0x38f93ac5 0x37aec037 0x355aed91 0x3710c1e5 0x372ded6b 0x372e0f03 0x372e0e1d 0x372e0cff 0x372d64dd 0x372e0c3d 0x372e094f 0x372e03df 0x372e0237 0x372e0115 0x370a97bd 0x370a9135 0x370a8d8f 0x370a8d51 0x370a8c83 0x37260d17 0x372dff2b 0x372dfe5d 0x3449d689 0x372d520b 0x344c35ef 0x38fcf041 0x38fceff9 0x2950f0d 0x38f9c151 0x38f9a049 0x292f711 0x38f998b7 0x38f983ad 0x294dc4f 0x38f97737 0x38e7f5f9 0x38e6c809 0x38e6c123 0x3801f5a3 0x3801f1d3 0x37b70173 0x37b70117 0x37b6ef99 0x37ae1ebd 0x37ae1d49 0x3801e2eb 0x38ec0301 0x6190d 0x593e8) 
libc++abi.dylib: terminate called throwing an exception 

我不確定在哪裏出現這種出界異常發生在,我會很感激任何評論。

+0

你有一個斷點異常? – FluffulousChimp

+0

你可以發佈所有的輸出?什麼是** self.sourceArray **? – Pablosproject

+0

你沒有在這裏發佈的是你的「tableView:cellForRowAtIndexPath:」例程。這可能會告訴我們在哪裏崩潰。 –

回答

0

你用委託方法

  • (無效)searchBarTextDidBeginEditing:(*的UISearchBar)搜索欄;
  • (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
  • (void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText fbFriendList_table;
  • (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;