2013-09-23 28 views
0

我创建了自动完成文本框我的问题是,如何传递数组存储的值从分支 回收当使用_category详细信息我的应用程序将崩溃。 如果我用NSArray与对象没有问题。 当我调试的代码,我发现问题,寻找方法plz帮助解决我的问题 谢谢你这么多如何将db的值传递给自动完成文本字段?

我的功能

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

立即查看现场图像的 when edit textbox array value view in table view

+1

发布您的崩溃报告! – Praveenkumar

回答

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子句here

一旦执行完成,你会得到你的最终结果。只需找到sample project here

相关问题