How to get actual value of NSMutableArray not Inde

2019-09-07 05:18发布

问题:

I am new in iOS and I am facing a problem regarding to get the ID of array after searching. I need to get the actual value of array after searching but when I search the content of array it give me the index of array which is at present in tableview. My code is like this :

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    arrOfColor=[NSMutableArray arrayWithObjects:@"Red",@"Green",@"Blue",@"Gray",@"Black",@"White",@"Yellow",@"Brown",@"Pink",nil];  //Hear arrofColor is NSMutableArray.
    idarray2 =[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; //Hear idarray is NSMutableArray.

    [self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(isFilter)
    {
        return [searchArray count];
    }
    else
        return  [arrOfColor count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if(isFilter)
    {
        cell.textLabel.text=[searchArray objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text=[arrOfColor objectAtIndex:indexPath.row];
    }

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(isFilter)
    {
        _searchTextField.text=[searchArray objectAtIndex:indexPath.row];
        idlbl.text=[idarray2 objectAtIndex:indexPath.row];

    }
    else
    {
        _searchTextField.text=[arrOfColor objectAtIndex:indexPath.row];
        idlbl.text=[idarray2 objectAtIndex:indexPath.row];

    }

}

-(void)textFieldDidChange:(UITextField *)textField
{
    searchTextString=textField.text;
    [self updateSearchArray:searchTextString];
}
-(void)updateSearchArray:(NSString *)searchText
{
    if(searchText.length==0)
    {
        isFilter=NO;
    }
    else{

        isFilter=YES;
        searchArray=[[NSMutableArray alloc]init];
        for(NSString *string in arrOfColor){

            NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(stringRange.location !=NSNotFound){

                [searchArray addObject:string];
            }
        }
        [self.colorTableview reloadData];}
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

Before search value of array is 6

But When I search it it's ID change to 1.

So, How can I get the 6 id even after search. Thanks in Advance!

回答1:

You have to create a new array. A sample code:

NSMutableArray *arrOfColor=[NSMutableArray arrayWithObjects:@"Red",@"Green",@"Blue",@"Gray",@"Black",@"White",@"Yellow",@"Brown",@"Pink",nil];  //Hear arrofColor is NSMutableArray.
NSMutableArray *idarray2 =[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];

NSMutableArray *searchArrayColor    =[[NSMutableArray alloc]init];
NSMutableArray *searchArrayId       =[[NSMutableArray alloc]init];


for(NSString *string in arrOfColor){

    NSRange stringRange=[string rangeOfString:@"r" options:NSCaseInsensitiveSearch];
    if(stringRange.location !=NSNotFound){
        [searchArrayColor addObject:string];

        NSInteger index = [arrOfColor indexOfObject:string];

        [searchArrayId addObject:[idarray2 objectAtIndex:index]];
    }
}

NSLog(@"%@",arrOfColor);

/*2016-10-14 12:15:44.618 objC[1855:50348] (
                                          Red,
                                          Green,
                                          Blue,
                                          Gray,
                                          Black,
                                          White,
                                          Yellow,
                                          Brown,
                                          Pink
                                          )*/

NSLog(@"%@",idarray2);

/*2016-10-14 12:15:44.619 objC[1855:50348] (
                                          1,
                                          2,
                                          3,
                                          4,
                                          5,
                                          6,
                                          7,
                                          8,
                                          9
                                          )*/

NSLog(@"===========");

NSLog(@"%@",searchArrayColor);
/*
 2016-10-14 12:15:44.619 objC[1855:50348] (
 Red,
 Green,
 Gray,
 Brown
 )
 */


NSLog(@"%@",searchArrayId);
/*
 2016-10-14 12:15:44.619 objC[1855:50348] (
 1,
 2,
 4,
 8
 )
 */


回答2:

so you are showing that id like,

   idlbl.text=[idarray2 objectAtIndex:indexPath.row]; 

as you said in comment in your question then it will definitely prints 1 because after search your tableview have only one row so, indexPath.row will be 0, so [idarray2 objectAtIndex:indexPath.row]; means first object of idarray2 and it is 1. So, it will return 1.

Now if you want relevant id then you can do something like,

  NSUInteger index = [arrOfColor indexOfObject:@"white"];   // pass color here i have take static value for demonstration you should pass dynamic value depend on search


  NSString *yourId  = [idarray2 objectAtIndex:index];

   idlbl.text=yourId;