1

我正在 iPhone 中完成 AutoCompleteTextView 的概念,类似于我们在进行 safari 时发现的。我是在 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"])

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

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 回答