0

I want to use tableview + searchDisplay with two different source, that is, when user attempts to search on searchbar, it should searches from different an array, and display it.

The way I can do this is, distinguish source in table view delegate and treat different source when searchdisplay call table view delegate methods. So I did something like:

 - (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; 
}

but once I attempted to search on search bar ( as soon as text changed ), it just crashed with index out of bounds. I tried to debug it but couldn't find what is really wrong.

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

I'm not sure where this out of bound exception occur at, I'd appreciate any comment.

4

1 回答 1

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;
于 2012-10-09T09:48:22.647 回答