我的应用程序崩溃后,如果我这样做:应用程序崩溃的UITableView搜索
我做一个搜索我的UITableView
搜索我轻按
searchDisplayController
的UITableView的UITableViewCell的,去到另一个查看后控制器然后我回到主要的UITableView,
questionTable
- 我在其中搜索,然后点击最后一个单元格或任何单元格,这个单元格大于我以前的搜索结果数量。应用程序崩溃与此错误:
*** 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];
}
}
你可以发布didSelectRowAtIndexPath:?听起来就像是碰撞发生的地方?为了确定,请设置一个所有例外断点(左侧的项目导航器,顶部的断点标签,'+'加号,底部的'添加异常断点')。请评论它停止的行。 – danh
问题是您的数组为零,或为空 – MCKapur
将主数组中的元素添加到searchArray时,是否使用了深层复制?第二,当你回来时,请在视图中重新加载表格,然后应用程序崩溃或不?告诉我。 –