【问题标题】:Using a Search Bar through an sqlite database通过 sqlite 数据库使用搜索栏
【发布时间】:2012-07-15 07:21:39
【问题描述】:

我正在构建一个从 SQLite 数据库加载数据的 iPhone 应用程序。到目前为止,我已经设法处理了它,但现在我需要在顶部添加一个搜索栏来动态搜索数据库内容并获得正确的搜索结果。

我使用的是 Xcode 4.3.3,我是 Xcoding 的新手。

我几乎看过网上发布的所有教程,我需要一些帮助。

谁有任何非常具体的教程或示例代码可以提供帮助?将不胜感激。

【问题讨论】:

  • 哪个部分造成了麻烦,处理uisearchbar事件或查询db?
  • 实际上将查询传递到搜索栏

标签: ios xcode uitableview sqlite uisearchbar


【解决方案1】:

【讨论】:

    【解决方案2】:

    您可以使用 NSPredicate。 http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

    [NSPredicate predicateWithFormat:@"whatYouAreSearching LIKE %@", searchBar.text];
    

    如果您使用的是 NSFetchRequest,您可以使用它的 setPredicate 方法为其分配一个谓词。

    【讨论】:

      【解决方案3】:

      你的控制器必须实现 UISearchBarDelegate 协议

      然后在init方法中,添加这个或类似的

      CGRect searchBarRect = CGRectMake(0, 0, self.view.bounds.size.width, 44);
      _searchBar = [[UISearchBar alloc] initWithFrame:searchBarRect];
      _searchBar.delegate = self;
      

      之后,实现这三个方法,我会发布一些看起来像我的实现的东西

      - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
          [searchBar setShowsCancelButton:YES animated:YES];
          self.tableView.allowsSelection = NO;
          self.tableView.scrollEnabled = NO;
      }
      
      - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
          searchBar.text=@"";
          [searchBar setShowsCancelButton:NO animated:YES];
          [searchBar resignFirstResponder];
          self.tableView.allowsSelection = YES;
          self.tableView.scrollEnabled = YES;
      }
      
      - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
      
          [searchBar setShowsCancelButton:NO animated:YES];
          [searchBar resignFirstResponder];
          self.tableView.allowsSelection = YES;
          self.tableView.scrollEnabled = YES;
      
          /* fire method that does querying with searchBar.text as attribute */
          [self some method:searchBar.text];
      }
      

      【讨论】:

      • 首先感谢您的回复,其次,查询的方法,我如何才能真正创建它...按照几个教程我迷路了,如何进行自动搜索和过滤数据?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      相关资源
      最近更新 更多