2012-08-23 67 views
2

我的应用程序崩溃后,如果我这样做:应用程序崩溃的UITableView搜索

  1. 我做一个搜索我的UITableView

  2. 搜索我轻按searchDisplayController的UITableView的UITableViewCell的,去到另一个查看后控制器

  3. 然后我回到主要的UITableView,questionTable - 我在其中搜索,然后点击最后一个单元格或任何单元格,这个单元格大于我以前的搜索结果数量。

  4. 应用程序崩溃与此错误:

    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 12 beyond bounds [0 .. 0]' 
    *** First throw call stack:(0x35aa188f 0x37e48259 0x359ea9db 0x155fd 0x3384df5b 0x153ed 0x3358793d 0x33601627 0x355bb933 0x35a75a33 0x35a75699 0x35a7426f 0x359f74a5 0x359f736d 0x37693439 0x33503cd5 0x95a3 0x9548) terminate called throwing an exception(lldb) 
    

我怀疑,问题是我的代码,因为搜索的UITableView对我来说是一个新课题。这就是我要做的事:

1.My VC符合这些协议<UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>

2.我有这些变量:

@property (nonatomic, retain) NSMutableArray *searchResults; 
@property (nonatomic, copy) NSString *savedSearchTerm; 

3.在viewDidLoad设置我的UITableView的来源,我搜索:

self.questionList = [[NSArray alloc] 
        initWithObjects: MY OBJECTS HERE, nil]; 

if ([self savedSearchTerm]) 
{ 
    [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]]; 
} 

4.我有这样的行动来处理搜索:

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

if ([self searchResults] == nil) 
{ 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [self setSearchResults:array]; 
} 
[[self searchResults] removeAllObjects]; 

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

5)这里就是我建立了我的UITable观点:

(UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    NSInteger row = [indexPath row]; 
    NSString *contentForThisRow = nil; 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
     contentForThisRow = [[self searchResults] objectAtIndex:row]; 
    else 
     contentForThisRow = [[self questionList] objectAtIndex:row]; 

    static NSString *CellIdentifier = @"questionsCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    [[cell textLabel] setText:contentForThisRow]; 
    return cell; 
    } 

6)这是我的numberOfRowsInSection方法看起来像:

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSInteger rows; 

if (tableView == [[self searchDisplayController] searchResultsTableView]) 
rows = [[self searchResults] count]; 
else 
rows = [[self questionList] count]; 
return rows; 
} 

7)最后,我有以下两种方法:

(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 questionTable] reloadData]; 
} 

对于长期问题,我真的很抱歉。我在代码中犯了一些错误,我希望有人能告诉我正确的做法。搜索栏控制器正确链接到IB 任何帮助将不胜感激!

新的编辑: 我在didSelectRowAtIndePath推一个新的VC:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"toAnswer" sender:indexPath]; 
} 

而且我更新一个NSString那里。它工作完美时,我只用一个TableView

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    ((QandADetailsViewController *)segue.destinationViewController).delegate=self; 

    NSIndexPath *indexPath = (NSIndexPath *)sender; 

    QandADetailsViewController *mdvc = segue.destinationViewController; 

    if (self.searchResults == nil) { 
     mdvc.questionName = [self.questionList 
          objectAtIndex:indexPath.row]; 
    } else { 
     mdvc.questionName = [self.searchResults 
          objectAtIndex:indexPath.row]; 
    } 
} 
+0

你可以发布didSelectRowAtIndexPath:?听起来就像是碰撞发生的地方?为了确定,请设置一个所有例外断点(左侧的项目导航器,顶部的断点标签,'+'加号,底部的'添加异常断点')。请评论它停止的行。 – danh

+0

问题是您的数组为零,或为空 – MCKapur

+0

将主数组中的元素添加到searchArray时,是否使用了深层复制?第二,当你回来时,请在视图中重新加载表格,然后应用程序崩溃或不?告诉我。 –

回答

3

在对SEGUE准备,为SearchResult所检查==零可能不正确。没有理由期望它在第一次搜索后永远不会有。这意味着您将始终在随后的表格选择中取消引用搜索数组,解释索引超出范围。

我认为这个修复方法是询问if ([self.searchDisplayController isActive])而不是搜索结果是否存在。

+0

当我尝试实现代码时出现此错误:''UISearchDisplayController'没有可见的@interface声明选择器'active '有没有解决办法? –

+0

应该是'if([self.searchDisplayController isActive])',它的工作原理!非常感谢你,男人! –

+0

很高兴听到。对不起,这个错误。我认为主动是一个属性,可以用点来访问。我会根据你的建议进行编辑。 – danh

1

看来你的数组是空的。

我会建议你在你做的每一步之后记录你的数组,所以你可以看到它在哪里变空。

也记录了行号和方法名称,看到这种情况,如何登录:

NSLog(@"%s\n [Line: %d]\n array:%@\n\n", __PRETTY_FUNCTION__, __LINE__, yourArray);