1

私はデモの天気アプリを作成しています。この中でUISearchBarを使用しているため、ユーザーは都市名を入力できますが、その提案を表示する方法がわかりません。

たとえば、ユーザーが「lon」と入力すると、ロンドンなどのように「lon」から始まる都市名が表示されるはずです。

4

2 に答える 2

1

UISearchBar独自の委任プロトコルUISearchBarDelegate、メソッドがあります -

-(BOOL)searchBar:(UISearchBar *)searchBar
shouldChangeTextInRange:(NSRange)range
  replacementText:(NSString *)text

編集中にいくつかの追加操作を実行できるようにします。ここではNSPredicate、挿入されたテキストに都市BEGINSWITHまたは入力されたテキストがあるかどうかを確認できCONTAINSます。

于 2015-04-01T07:15:12.277 に答える
0

あなたはそれを行うことができます、

  1. 表示する場所の配列 (リスト) を作成します ... 配列にオブジェクトを追加します!

    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);



}
  1. 次に、 numberOfRowsInTableview のリストをカウントします。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [listFiles count]; }

  1. cellForRow ::::

-(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];

お役に立てば幸いです.....

于 2015-04-01T09:07:07.423 に答える