0
我有NSarray,我想搜索根據我的數組中的所有鍵。我已應用此代碼。首先這是我的陣列。如何在iOS中搜索Nsarray?
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";
}
)
在上述我展示例如陣列的索引少現在這是陣列是我在UItable視圖中使用,並希望像付款,付款日期,狀態根據每個陣列鑰匙搜索等。我搜索成功關於所有密鑰,但我認爲這不是好方法。我在這裏顯示代碼是我用過的。
- (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];
}
此代碼成功地工作,但假設,當我們搜索像PAY-0177 ....在我的數組這一個第二個指標,現在根據搜索我們需要做這個第一指標。所以我這樣做也成功了,這裏是我的代碼。我在這裏顯示的代碼較少。所以請忽略所有的東西。
- (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];
}
}
根據這我得到特定的索引,我想要的。但我認爲我的數組中只有四個鍵現在假設如果我有10個我們的15個鍵,那麼我認爲這是不好的方法。所以請告訴我。
查找到[NSPredicate](https://developer.apple.com/librarY/mac/documentation/可可/參考/基金/班/ NSPredicate_Class/index.html的)。 – Michael 2014-10-31 05:11:00
我已經使用過這個請在我的搜索代理中查看。 – rahul 2014-10-31 05:17:54