iPhone中的UITextField自动完成功能?

时间:2012-11-10 06:19:52

标签: iphone xcode uitableview autocompletetextview

我正在iPhone中完成AutoCompleteTextView的概念,类似于我们去野生动物园时发现的......我是在UITableView的帮助下做的。但我得到错误“程序接收信号SIGABRT”无法理解我在哪里我错了..?

我的数组看起来像这样:

(
        {
        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"])

我哪里出错..?任何帮助将不胜感激..

2 个答案:

答案 0 :(得分: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] 
     }  
}

答案 1 :(得分: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];  
        }
    }