Rahul Sharma
Rahul Sharma

Reputation: 950

How to search From Nsarray in iOS?

I have NSarray and I want to search According to all keys which are in My array. I have applied this code . First of all this is my array .

   MyArray =     (
            {
        Payment = "PAY-0172595";
        "Payment Date" = "2014-10-29";
        "Payment ID" = a1Wo00000011uMMEAY;
        Status = Paid;
        "Total Amount" = "217.76";
    },
            {
        Payment = "PAY-0177591";
        "Payment Date" = "2014-10-30";
        "Payment ID" = a1Wo00000011w7uEAA;
        Status = Paid;
        "Total Amount" = "100.00";
    }
  ) 

In above I'm showing less index of array for example now this is array which are I'm used in UItable View and want to search according to each of array Keys like Payment,Payment Date,Status etc. I am search successfully regarding to all keys but I think this is not good way .I'm showing here code which are I'm used.

  - (void)viewDidLoad
  {

  for (int i=0; i<Payments_details.count; i++)
   {


  NSString *str=[NSString stringWithFormat:@"%@/%@/%@/%@/%@",[[Payments_details objectAtIndex:i] valueForKey:@"Payment"],[[Payments_details objectAtIndex:i] valueForKey:@"Total Amount"],[[Payments_details objectAtIndex:i] valueForKey:@"Payment Date"],[[Payments_details objectAtIndex:i] valueForKey:@"Status"],[[Payments_details objectAtIndex:i] valueForKey:@"Payment ID"]];

    NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:str,@"SN", nil];

    [search_arr addObject:dict];

 }

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
 {
 if (![search_bar.text isEqualToString:@""])
 {

    searching=YES;

   NSPredicate *resultPredicate = [NSPredicate
                                   predicateWithFormat:@"SELF contains[cd] %@",
                                   searchBar.text];
   //  NSLog(@"tim %@",resultPredicate);
   searchArr = [[search_arr valueForKey:@"SN"] filteredArrayUsingPredicate:resultPredicate];
   NSLog(@"tim %@",searchArr);

}
else
{
    searching=NO;

}
[table_Payments reloadData];

}

This code work successfully but suppose when we search like PAY-0177.... In my array this one 2nd index, now according to search we need to make this one first index. So I doing this also successfully here is my code .I am Showing here less code .So please ignore all things.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
   if (searching==YES)
    {

        NSArray* foo = [[searchArr objectAtIndex:indexPath.row] componentsSeparatedByString: @"/"];

        custom_cell.Payment.text = [foo objectAtIndex: 0];

       custom_cell.Totalamount.text=[foo objectAtIndex: 1];

       custom_cell.PaymentDate.text=[foo objectAtIndex: 2];

        custom_cell.Status.text=[foo objectAtIndex: 3];


    }

 }

according to this I'm get Particular index which I want. but I think there is only four keys in My array Now suppose If I have 10 our 15 Keys then I think this is not good approach .So Please tell me .

Upvotes: 0

Views: 642

Answers (1)

Related Questions