2012-03-29 60 views
1

我想问一个问题。 现在我正在使用UISearchBar,TableView和Sqlite在iOS中进行培训。 我有sqlite数据库,我想从该sqlite数据库中搜索数据,然后我想在tableView中显示搜索数据。 我已经在ViewDidLoad事件中将数据选择到名为myArray的NSMutableArray中,如下面的代码。如何在iOS中使用UISearchBar从sqlite搜索数据?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    MyInfo *myInfo = [[MyInfo alloc] init]; 
    myArray = [myInfo getInitialData]; // myArray is my NSMutableArray's variable name and getData From getInitialData Method. 

    [tableData addObjectsFromArray:myArray]; // tableData is my next NSMutableArray's variable name that i want to show search data in tableView. 
} 

像那样。我将myArray中的数据添加到tableData中。 然后我在UISearchBar的textDidChange Events中编写了一些代码。从database.There

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if([searchText isEqualToString:@""] || searchText==nil) 
    { 
     [myTableView reloadData]; 
     return; 
    } 

    NSInteger counter = 0; 
    for(NSString *nameMe in myArray) 
    { 
     NSRange r = [[nameMe lowercaseString] rangeOfString:[searchText lowercaseString]]; 
     if(r.location != NSNotFound) 
     { 
      if(r.location== 0)//that is we are checking only the start of the names. 
      { 
       [tableData addObject:nameMe]; 
      } 
     } 

     counter++; 
    } 
    [myTableView reloadData]; 
} 

但我不能搜索的数据是错误的发生。 我认为我在我的搜索代码中是错误的。 我的列名称是我想要从数据库中搜索的“名称”。 但是我不知道如何用UISearchBar搜索数据。 如果我的代码有误,请指教我如何从sqlite中搜索数据。 我只是iOS的初学者,请帮助我。

最好的问候,

+1

如果有样品的iOS项目的UISearchBar用的UITableView和SQLite数据库,请分享我!谢谢 – 2012-03-29 15:49:43

回答

2

这里是Link这是做exaclty什么ü想做的事情。只有你必须修改只是在示例中的表视图充满了plist元素(纠正我,如果我错了,我用它很长时间回来)

所有你必须做的是,用数组填充数组Sqlite数据库。我本来所示的U是很好,但我没有我的Mac上我现在

不要让我知道它的工作或u需要任何其他帮助...... !!!!

Cheeers!

更新

你分配了你的NSMutatableArray吗?

有些东西一样

NSMutableArray *myArray = [[NSMutableArray alloc] init]; 
+0

谢谢你这么多!我会尽help.If您可以稍后,请教我你的代码。再次感谢。 – 2012-03-29 17:24:46

相关问题