简体   繁体   English

UITableView搜索后应用崩溃

[英]App crash after UITableView search

My app crashes if I do this: 如果执行此操作,我的应用程序将崩溃:

  1. I make a search on my UITableView 我在UITableView上进行搜索

  2. After a search I tap on UITableViewCell of searchDisplayController UITableView and go to another View Controller 搜索后,我点击searchDisplayController UITableView的UITableViewCell并转到另一个视图控制器

  3. I then come back to the main UITableView, questionTable - the one in which I searched, and tap on the last cell, or any cell that is greater then the number of search results I previously had. 然后,我回到主要的UITableView, questionTable我在其中搜索的内容,然后点击最后一个单元格,或者点击大于我之前拥有的搜索结果数量的任何单元格。

  4. App crashes with this error: 应用程序因以下错误而崩溃:

     *** 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) 

I suspect the problem is with my code, because searching UITableView is a new topic for me. 我怀疑问题出在我的代码上,因为搜索UITableView对我来说是一个新主题。 That's how I do it: 我就是这样的:

1.My VC conforms to these protocols <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate> 1.我的VC符合以下协议<UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>

2.I have these variables: 2.我有这些变量:

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

3. In viewDidLoad i set up the source of my UITableView which I search: 3.在viewDidLoad我设置了要搜索的UITableView的源:

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

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

4. I have this action to handle the search: 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) Here's how I set up my UITable views: 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) Here's how my numberOfRowsInSection method looks like: 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) Finally, I have these two methods: 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];
}

I'm really sorry for the long question. 对于这个漫长的问题,我感到非常抱歉。 I have made some mistake in the code and I hope someone could show me the right way to do this. 我在代码中犯了一些错误,希望有人可以向我展示正确的方法。 The Search Bar Controller is properly linked in the IB Any help would be highly appreciated! 搜索栏控制器已在IB中正确链接。非常感谢您的帮助!

NEW EDIT: I'm pushing a new VC on didSelectRowAtIndePath : 新编辑:我在didSelectRowAtIndePath上推送了一个新的VC:

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

And I'm updating an NSString there. 我在那里更新了NSString。 It worked perfectly when I used just one TableView 当我只使用一个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];
    }
}

In prepare for segue, the check for searchResults == nil is probably incorrect. 在准备segue时,对searchResults == nil的检查可能不正确。 There's no reason to expect it to be nil ever after the first search. 首次搜索后,没有理由期望它为零。 That means you'll always be dereferencing the search array on subsequent table selections, explaining the index out of bounds. 这意味着您将始终在后续的表选择中取消引用搜索数组,从而说明索引超出范围。

The fix, I think, is to ask if ([self.searchDisplayController isActive]) instead of whether search results exist. 我认为解决方法是询问if ([self.searchDisplayController isActive])而不是是否存在搜索结果。

it seems that your array is empty. 看来您的阵列是空的。

i would recommend you to log your array after every step you do, so you can see where it gets empty. 我建议您在执行每个步骤后记录阵列,以便可以看到阵列为空。

also log the line number and method names, see this, how to log: 还记录行号和方法名称,请参阅此方法,如何记录:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM