0

我创建了自动完成文本框,我的问题是如何传递数组是从 db 检索的存储值当使用 _category 详细信息时,我的应用程序将崩溃。如果我将 NSArray 与对象一起使用,则没有问题。当我调试代码时,我在搜索方法中发现问题请帮助解决我的问题非常感谢

我的功能

- (void)viewDidLoad
{
     self.categoryDetail = [CompanyDetailDatabase database].categoryDetail;

    NSMutableArray *arrt = [[NSMutableArray alloc]init];
    //arrt = [NSMutableArray arrayWithArray:_categoryDetail];
    arrt=[NSArray arrayWithArray:_categoryDetail];

    self.pastUrls = [[NSMutableArray alloc]initWithArray:arrt];
   // self.pastUrls = [[NSMutableArray alloc] initWithObjects:@"Hello1",@"Hello2",@"Hello3", nil];
    self.autocompleteUrls = [[NSMutableArray alloc] init];

    autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 180, 280, 50) style:UITableViewStylePlain];
    autocompleteTableView.delegate = self;
    autocompleteTableView.dataSource = self;
    autocompleteTableView.scrollEnabled = YES;
    autocompleteTableView.hidden = YES;
    [self.view addSubview:autocompleteTableView];

    [txtProduct setDelegate:self];
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

    // Put anything that starts with this substring into the autocompleteUrls array
    // The items in this array is what will show up in the table view
    [autocompleteUrls removeAllObjects];
    for(NSString *curString in pastUrls)
    {
        NSRange substringRangeLowerCase = [curString rangeOfString:[substring lowercaseString]];
        NSRange substringRangeUpperCase = [curString rangeOfString:[substring uppercaseString]];
        if (substringRangeLowerCase.length !=0 || substringRangeUpperCase.length !=0)
        {
            [autocompleteUrls addObject:curString];
        }
    }
    autocompleteTableView.hidden =NO;
    [autocompleteTableView reloadData];
}

#pragma mark UITextFieldDelegate methods

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    autocompleteTableView.hidden = NO;

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

#pragma mark UITableViewDataSource methods

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

#pragma mark UITableViewDelegate methods

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

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

现在查看场景图像 在表格视图中编辑文本框数组值视图时

4

1 回答 1

0

我不确定你的方法。但是,实现起来非常简单。首先,您必须在数据库表中插入一些记录。而且,在对您进行编辑时,您UITextField必须将文本作为sqlite 查询与您的条件一起传递到您的数据库表并返回为NSArrayNSMutableArray一些代码片段 -

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == listTableView)
    {
        recordsArray = [self getRecords:string];

        if ([recordsArray count] == 0 || recordsArray == NULL)
        {
            [[[UIAlertView alloc] initWithTitle:nil message:@"No Records Were found" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil] show];
        }
        else
        {
            tableV.hidden = NO;
            [tableV reloadData];
        }
    }
    return YES;
}

上面将获取您输入的字符串并传递给名为getRecords的数据库方法和输入的字符串。

-(NSMutableArray*)getRecords:(NSString *)srchString
{
    NSMutableArray *mutArray = [[NSMutableArray alloc]init];

    sqlite3 *servicesDB;
    NSString *filePath=[AppDelegate dbPath];

    if(sqlite3_open([filePath UTF8String], &servicesDB) == SQLITE_OK)
    {
        NSString *sqlStatement;
        sqlStatement = [NSString stringWithFormat:@"SELECT * FROM Records WHERE record = '%%%@%%'", srchString];
        const char *sql = [sqlStatement UTF8String];

        sqlite3_stmt *select_statement;
        if (sqlite3_prepare_v2(servicesDB, sql, -1, &select_statement, nil) == SQLITE_OK)
        {
            while (sqlite3_step(select_statement) == SQLITE_ROW)
            {
            char *record = (char *)sqlite3_column_text(select_statement, 0);

            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

            [dict setObject:[NSString stringWithUTF8String:record] forKey:@"record"];

            [mutArray addObject:[dict copy]];
        }
    }
    else
    {
        NSLog(@"Error while fetching data. '%s'", sqlite3_errmsg(servicesDB));
    }
            sqlite3_finalize(select_statement);
    }
    sqlite3_close(servicesDB);
    return mutArray;
}

上面的数据库方法将执行并获取所有包含单词的数据,无论您输入什么。您可以在此处找到有关LIKE 子句的详细信息

执行完成后,您将获得最终结果。只需在此处找到示例项目

于 2013-09-26T13:16:45.713 回答