1

私はiPhoneでAutoCompleteTextViewの概念を達成しています.サファリに行くときに見つけるのと同じように..私はUITableViewの助けを借りてそれをやっています..?

私の配列は次のようになります:

(
        {
        Name = "John";
    },
        {
        Name = "Williams ";
    },
        {
        Name = "Michael ";
    },
        {
        Name = "Hillary ";
    },
        {
        Name = "Jennifer ";
    },
)

そのため、tableView のセルで Name の値を取得する必要があります。

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {


    [autocompleteUrls removeAllObjects];
    for(int i=0;i<[arr2 count];i++)
    {
        for(NSString *curString in [[arr2 objectAtIndex:i] valueForKey:@"Name"]) //error :Program recieved signal SIGABRT
       {
        NSRange substringRange = [curString rangeOfString:substring];
        if (substringRange.location == 0) 
        {
            [autocompleteUrls addObject:curString];  
        }
       }
    }
    [autocompleteTableView reloadData];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


    if( textField == txtcity)
    {
        [txtcity resignFirstResponder];
    autocompleteTableView.hidden = NO;

    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return autocompleteUrls.count;
}

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

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
    }

    cell.textLabel.text = [autocompleteUrls objectAtIndex:indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    txtcity.text = selectedCell.textLabel.text;

}

この行でエラーが発生しています:for(NSString *curString in [[arr2 objectAtIndex:i] valueForKey:@"Name"])

どこが間違っているのでしょうか..?どんな助けでも大歓迎です..

4

2 に答える 2

0

試す:

for(NSString *curString in arr2)

また

for(int i=0;i<[arr2 count];i++)
{
     NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"]
     curString = [curString lowercaseString];
     substring = [substring lowercaseString];

     if ([curString rangeOfString:substring].location == NSNotFound) 
     {} 
     else 
     { 
          [autocompleteUrls addObject:curString] 
     }  
}
于 2012-11-10T06:38:15.917 に答える
0

[[arr2 objectAtIndex:i] valueForKey:@"Name"]配列ではなく、文字列(名前)を返します。したがって、forループは必要ありません。

返す名前を変数に割り当てるだけです。

for(int i=0;i<[arr2 count];i++)
    {
        NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"]

        NSRange substringRange = [curString rangeOfString:substring];
        if (substringRange.location == 0) 
        {
            [autocompleteUrls addObject:curString];  
        }
    }
于 2012-11-10T06:44:51.073 に答える