私はそこから文字列を取得するために自分のWebサービスにアクセスし、AutoCompleteTableView
以下の方法でそれを自分の中に入れようとしています-
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *substring = [NSString stringWithString:txtSearching.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
if (([substring isEqualToString:@""]) || (substring == nil) || ([substring length] == 0)){
autoCompleteTableView.hidden=YES;
}
NSString *jsonUrl = [NSString stringWithFormat:MYAPI,substring];
NSURL *url = [NSURL URLWithString:[jsonUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"]];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSArray *data = [jsonData objectFromJSONData];
for (int i = 0; i< [data count]; i++) {
NSDictionary * dic = [data objectAtIndex:i];
[elementArray addObject:[dic objectForKey:@"keyword"]];
}
[self searchAutocompleteEntriesWithSubstring:substring];
[elementArray removeAllObjects];
return YES;
}
と私searchAutocompleteEntriesWithSubstring
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
// Put anything that starts with this substring into the autoCompleteArray
// The items in this array is what will show up in the table view
//[elementArray removeAllObjects];
[autoCompleteArray removeAllObjects];
int lengthOfSubstring = [substring length];
for(NSString *curString in elementArray) {
int lengthOfCurentString = [curString length];
if (lengthOfCurentString >=lengthOfSubstring) {
NSString *substringRangeLowerCase = [[curString substringWithRange:NSMakeRange(0,[substring length])] lowercaseString];
NSString *substringRangeUpperCase = [[curString substringWithRange:NSMakeRange(0,[substring length])] uppercaseString];
NSString *substringRangeCase = [[curString substringWithRange:NSMakeRange(0,[substring length])] capitalizedString];
if (([substring isEqualToString:@""]) || (substring == nil) || ([substring length] == 0)) {
// NSLog(@"current string = [%@]",curString);
}else{
if (([substring isEqualToString:substringRangeLowerCase]) || ([substring isEqualToString:substringRangeUpperCase]) || ([substring isEqualToString:substringRangeCase])) {
[autoCompleteArray addObject:curString];
}
}
}
}
autoCompleteTableView.hidden = NO;
[autoCompleteTableView reloadData];
}
私のデータ-
[{"keyword": "qwerty"}、{"keyword": "question paper judicial"}、{"keyword": "qwe"}、{"keyword": "quilts"}、{"keyword": "quinoa "}、{"キーワード ":" qwerty0disk "}、{"キーワード ":" q "}]
UI
なぜ非常に遅くなるのかわかりませんJSONKit
。また使用しています。