2012-03-29 61 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

相關問題