搜索时无法删除对象

时间:2016-01-07 07:51:33

标签: objective-c uitableview uisearchbar

我有一个包含两个部分的表格视图,所选项目位于第0部分,所有项目列表都在第1部分中。还有一个搜索栏,用于在表格视图中搜索项目。搜索功能正常。插入和删除功能在正常模式下也能正常工作。但如果我在搜索时删除对象,则会在

处崩溃
[searcheddataArray removeObjectAtIndex:indexPath.row];

这是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    Alllist = [[NSMutableArray alloc]initWithObjects:@"Ajmeri gate",@"Chawri bazar",@"Delhi Cant",@"VidhanSabha",@"Dhaula Kuan",@"Nehru Nagar", nil];

    selectedlist = [[NSMutableArray alloc]init];

    [aTableView setEditing:YES animated:YES];

    searcheddataArray = Alllist;

}



-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section==0)
    {
        NSUInteger count = [self.selectedlist count];
        if(self.editing) count++;
        return count;
    }
    else
    {
        return [searcheddataArray count];
    }

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return @"Selected List";
    if(section == 1)
        return @"All List";
    else
        return nil;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section==0)
    {

        cell.textLabel.text = [selectedlist objectAtIndex:indexPath.row];
    }
    else {

        cell.textLabel.text=[searcheddataArray objectAtIndex:indexPath.row];

    }
    return cell;
}




- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0)
    {
        return UITableViewCellEditingStyleDelete;

    }

    if (indexPath.section == 1)
    {

        return UITableViewCellEditingStyleInsert;
    }

    return UITableViewCellEditingStyleNone;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        [searcheddataArray insertObject:[selectedlist objectAtIndex:indexPath.row] atIndex:[searcheddataArray count]];

        [self.selectedlist removeObjectAtIndex:indexPath.row];

        [aTableView reloadData];
    }

    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {

        [self.selectedlist insertObject:[searcheddataArray objectAtIndex:indexPath.row] atIndex:[self.selectedlist count]];


            [searcheddataArray removeObjectAtIndex:indexPath.row];

        [aTableView reloadData];
    }
}



- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [self.aSearchbar setShowsCancelButton:YES animated:YES];
    [aTableView setEditing:YES animated:YES];

}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{

    [self.aSearchbar setShowsCancelButton:YES animated:YES];
    searchBar.text=@"";
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length>0)
    {
        searchText = [searchText stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"its pincode");

        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
        searcheddataArray= [Alllist filteredArrayUsingPredicate:resultPredicate];

        [aTableView reloadData];
    }
    else
    {
        searcheddataArray=Alllist;
        [aTableView reloadData];
    }
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.aSearchbar resignFirstResponder];
}



- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.aSearchbar resignFirstResponder];
    searcheddataArray=Alllist;

    [self.aTableView reloadData];
}



@end

我一定错过了一些东西,但我无法弄明白。任何意见都将不胜感激。

2 个答案:

答案 0 :(得分:0)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     //your code
}

While inserting or deleting the array objects in the above method you have to add tableview beginUpdate and tableview endUpdate methods.

See the below links, It may helps

http://stackoverflow.com/questions/23260279/what-is-the-benefit-of-calling-beginupdates-endupdates-for-a-uitableview-as-oppo

答案 1 :(得分:0)

异常消息:

[__NSArrayI removeObjectAtIndex:]:

表示您拥有(不可变NSArray而不是NSMutableArray

我无法从您发布的代码中看到原因,但是在某些时候您已将错误类型的对象分配给实例变量。