私はデモの天気アプリを作成しています。この中でUISearchBarを使用しているため、ユーザーは都市名を入力できますが、その提案を表示する方法がわかりません。
たとえば、ユーザーが「lon」と入力すると、ロンドンなどのように「lon」から始まる都市名が表示されるはずです。
私はデモの天気アプリを作成しています。この中でUISearchBarを使用しているため、ユーザーは都市名を入力できますが、その提案を表示する方法がわかりません。
たとえば、ユーザーが「lon」と入力すると、ロンドンなどのように「lon」から始まる都市名が表示されるはずです。
UISearchBar
独自の委任プロトコルUISearchBarDelegate
、メソッドがあります -
-(BOOL)searchBar:(UISearchBar *)searchBar
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
編集中にいくつかの追加操作を実行できるようにします。ここではNSPredicate
、挿入されたテキストに都市BEGINSWITH
または入力されたテキストがあるかどうかを確認できCONTAINS
ます。
あなたはそれを行うことができます、
表示する場所の配列 (リスト) を作成します ... 配列にオブジェクトを追加します!
NSArray *list;
NSArray *listFiles;
2.コンテンツをフィルタリングします..
-(void)filter :(NSString *) match1
listFiles = [[NSMutableDictionary alloc] init];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"FullName BEGINSWITH[c] %@", match1];
listFiles = [[ContactDect filteredArrayUsingPredicate:sPredicate ] mutableCopy];
f_name_array=[[NSMutableArray alloc]init];
for (NSDictionary *d in listFiles) {
[self.f_name_array addObject:[d objectForKey:@"FullName"]];
}
list=f_name_array;
NSLog(@"%@",list);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listFiles count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"abccell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@" ,[listFiles objectAtIndex:indexPath.row]];
return cell;
}
textFeild の編集中に Filter メソッドを呼び出します。
[self filter:textfeild1.text];
お役に立てば幸いです.....