一旦搜索开始,ios tableview就会崩溃

时间:2012-10-08 18:47:16

标签: iphone ios uisearchdisplaycontroller

我想使用tableview + searchDisplay和两个不同的源,也就是说,当用户尝试在搜索栏上搜索时,它应该从不同的数组中搜索并显示它。

我可以这样做的方法是,在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

我不确定这个超出范围的例外发生在哪里,我很感激任何评论。

1 个答案:

答案 0 :(得分:0)

您是否使用过代表方法

  • (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
  • (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
  • (void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText fbFriendList_table;
  • (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
相关问题