Ios NSMutableArray越界错误
我有一个方法的这一部分给了我一个错误。 这是一个单词过滤器,用户输入a,所有单词都会出现,等等 当用户删除搜索栏文本并单击表上的某个内容时,会出现错误,我得到越界异常 filteredListContent的打印输出是一个条目,我应该实现什么来防止程序崩溃 谢谢Ios NSMutableArray越界错误,ios,nsmutablearray,Ios,Nsmutablearray,我有一个方法的这一部分给了我一个错误。 这是一个单词过滤器,用户输入a,所有单词都会出现,等等 当用户删除搜索栏文本并单击表上的某个内容时,会出现错误,我得到越界异常 filteredListContent的打印输出是一个条目,我应该实现什么来防止程序崩溃 谢谢 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([_filteredListC
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([_filteredListContent count]>0){
NSLog(@"%i",indexPath.row);
NSLog(@"%@",_filteredListContent);
_searchBar.text=[self.filteredListContent objectAtIndex:indexPath.row];
//Save to user default
我猜您正在使用搜索显示控制器在表中搜索。但同时对这两个表使用相同的数组referenceself.filteredListContent一个是搜索结果表,另一个是用于显示所有单词的原始表。在搜索栏中键入字符时,您正在修改self.filteredListContent数组。这就是为什么当您搜索某个self.filteredListContent gets modifiedwill时,它包含的对象可能少于加载原始表时使用的对象,然后您选择原始表的任何行,如果您尝试访问数组中不存在的索引处的对象,它将崩溃
所以我建议你保留两个数组。一个用于加载原始表,一个用于搜索结果表,我不确定是什么导致了您的问题,但从给定的代码来看,所选行似乎超出了范围。我将更改我的支票:
[_filteredListContent count] > 0
例如:
[_filteredListContent count] > indexPath.row
同样,这只是基于给定的代码。下面是单词过滤器的简单示例
此代码在您的情况下可能很有用
谢谢:当用户删除搜索文本时,您做了什么?错误发生在哪一行?第4行\u searchbar.text…我知道这是一件愚蠢的事情。。。我永远也抓不到它,谢谢。这起作用了
-(void)viewDidLoad
{
self.listOfTemArray = [[NSMutableArray alloc] init];
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObject:@"/ArrayList/"];
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray];
}
#pragma mark -
#pragma mark Table view Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.listOfTemArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row];
return cell;
}
#pragma mark -
#pragma mark SearchBar methods
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
NSString *name = @"";
NSString *firstLetter = @"";
if (self.listOfTemArray.count > 0)
[self.listOfTemArray removeAllObjects];
if ([searchText length] > 0)
{
for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
{
name = [self.ItemOfMainArray objectAtIndex:i];
if (name.length >= searchText.length)
{
firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
//NSLog(@"%@",firstLetter);
if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
{
// strings are equal except for possibly case
[self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
NSLog(@"=========> %@",self.listOfTemArray);
}
}
}
}
else
{
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
}
[self.tblView reloadData];
}