0

私はこのようなデータを持っています...(すべてのデータは.plistファイルから来ています...)

Searching Array - (
            {
            FirstName = "Ramesh";
            LastName = "Bean";
            EmpCode = 1001;
        },
            {
            FirstName = "Rohan";
            LastName = "Rathor";
            EmpCode = 102;
        },
            {
            FirstName = "Priya";
            LastName = "Malhotra";
            EmpCode = 103;
        },
            {
            FirstName = "Mukesh";
            LastName = "Sen";
            EmpCode = 104;
        },
            {
            FirstName = "Priya";
            LastName = "Datta";
            EmpCode = 105;
        }

    )

FirstName(キー)に基づいてこの配列から検索データを実装したいと思います。

「FirstName(Key)」でデータを検索できます

しかし、データをフィルタリングした後、TableViewに表示されているRow(データ内)をクリックしたとします。その特定の従業員のすべての情報(FirstName、LastName、EmpCodeなど)を含むNew-Controllerに移動します。

どうすれば情報を入手できますか?

私が検索サンプルコードを調べたとき。

これが私の検索コードです...

NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];
    NSInteger TotalNoOfRecords=[self.SearchtableDataSource count];

    for (int i=0;i<TotalNoOfRecords;i++)
    {   NSDictionary *dictionary = [self.SearchtableDataSource objectAtIndex:i];
        NSArray *array = [dictionary objectForKey:@"FirstName"];


        [searchArray addObject:array];


    }
    for (NSString *sTemp in searchArray)
    {

        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
        {
                [copyListOfItems addObject:sTemp];

        }
    }

このコードを改善するにはどうすればよいですか?....ガイドしてください...[searchArrayリリース]; searchArray = nil;

searchArray内のすべての「Keys(FirstName、LastName、EmpCode)」をどのように維持するか教えてください。ありがとう...

4

2 に答える 2

0

これが私がしたことと私と一緒にうまく機能していることです(それをテストしました)。UISearchBarのIBOutletを1つ取得したので、ここにur.hファイルのコードを示します。

@interface SearchForEmp : UIViewController<'UISearchDisplayDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource> {

IBOutlet UISearchBar* mySearchBar;
UISearchDisplayController* mySearchDisplayController;

NSMutableArray* allEmp;
NSMutableArray* filteredEmp;
BOOL isFilatering;
}
@end

今.mファイルに

-(void)viewDidLoad 
{
[super viewDidLoad];

mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
mySearchDisplayController.searchResultsDelegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.delegate = self;

isFilatering = NO;
filteredEmp = [[NSMutableArray alloc] init];
allEmp = [[NSMutableArray alloc] init];
NSDictionary *temp;
for (int i = 0; i < 30; i++) {
    temp = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"FirstName_%d",i],@"FirstName",[NSString stringWithFormat:@"LastName_%d",i],@"LastName",[NSNumber numberWithInt:1000+i],@"EmpCode",nil];
    [allEmp addObject:temp];
}
}

たとえば、目的のために、それぞれが一意の名前を持つ30個のディクショナリの配列を作成しました。テーブルビューのデータソースとデリゲートの場合....。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;
}


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

{

if (isFilatering) {
    return [filteredEmp count];
}
else {
    return [allEmp count];
}
return [allEmp count];

}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] init] autorelease];
}

NSDictionary* temp;
if (isFilatering) {
    temp = [filteredEmp objectAtIndex:indexPath.row];
}
else {
    temp = [allEmp objectAtIndex:indexPath.row];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[temp objectForKey:@"FirstName"],[temp objectForKey:@"LastName"]];
return cell;
}

さて、検索に関する限り、ここであなたがしなければならないことは……。

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

NSPredicate* predicate = [[NSPredicate predicateWithFormat:@"self.FirstName contains %@",mySearchBar.text] retain];
filteredEmp = [[allEmp filteredArrayUsingPredicate:predicate] retain];

}

-(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller

{

isFilatering = YES;

}

-(void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller

{

isFilatering = NO;

}

私はあなたが物事を理解するのに役立つことを願っています、行に保持を追加することを忘れないでください filteredEmp = [[allEmp filteredArrayUsingPredicate:predicate] retain];

メソッドfilteredArrayUsingPredicate:はアクセサーメソッドであり、その保持カウントはNSArrayによって処理されるため、フィルター処理された配列にアクセスするには、保持メッセージを渡す必要があります。

于 2011-02-21T06:26:40.033 に答える
0

"FirstName""LastName""EmpCode"NSDictionaryという 3 つのキーの値を持つ配列を作成するのが最善の方法だと思います。

「FirstName」に従ってデータをフィルタリングするには、for ループの代わりに NSPredicate を使用します。

NSString *searchText = searchBar.text;

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"FirstName like[cd] %@",searchText];
NSArray* filteredArray = [self.SearchtableDataSource filteredArrayUsingPredicate:predicate];

メソッド cellForRowAtIndexPath 内

NSDictionary* currentEmp = [filteredArray objectAtIndex:inddexPath.row];

この currentEmp の情報を表示します。

同様にdidSelectRowAtIndexPath

NSDictionary* currentEmp = [filteredArray objectAtIndex:inddexPath.row];

現在の雇用の詳細を表示したい次のViewControllerにこの辞書を渡します。

(フィルタリングされた結果を表示する) ビューと結果全体を表示するビューの 2 つのテーブル ビューsearchDisplayControllerがある場合、フィルタリングがアクティブかどうかを示す BOOL 変数を追跡することで、これを追跡できます。

于 2011-02-18T14:15:49.870 に答える