【问题标题】:Help with UISearchBar, located in first cell of UITableView? Won't enter searchBar:textDidChange method帮助 UISearchBar,位于 UITableView 的第一个单元格?不会进入 searchBar:textDidChange 方法
【发布时间】:2011-08-08 09:10:22
【问题描述】:

今天我创建了一个 tableView,类似于 iPhone 联系人应用程序。我使用代码将 TableView 放入第一个单元格中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath indexAtPosition:0] == 0) {
        static NSString *CellIdentifier = @"SearchCell";
        UITableViewCell *searchBarCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        searchBar = [[UISearchBar alloc] initWithFrame:searchBarCell.frame];
        [searchBarCell addSubview:searchBar];
        return searchBarCell;
    } // ...

searchBar 显示正确,但是当我实现搜索方法时,我发现在 searchBar 中键入时它们没有被输入...例如这个方法:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTerm

我认为是因为 searchBar 是另一个类 tableView Cell 的子视图?这就是为什么搜索栏无法访问搜索方法的原因?我还将搜索栏委托设置为 self,仍然没有。

任何身体都可以帮忙吗? 提前非常感谢:)

【问题讨论】:

  • 当你说你将delegate设置为self时,对象self是什么?
  • 我不明白。我在 viewDidLoad 中做了这个; searchBar.delegate = self ...
  • 你能把那部分代码贴在这里吗?
  • 你是指搜索栏的方法吗?我做了 searchBar.delegate = self in view 确实加载了,我已经告诉过。
  • 没有调用委托方法吗?

标签: iphone ios uitableview uisearchbar


【解决方案1】:

您没有设置 UISearchBar 的委托。我在评论中读到你在其他部分做了,但你必须在创建对象后做。我的意思是在这行之后:

searchBar = [[UISearchBar alloc] initWithFrame:searchBarCell.frame]; 

【讨论】:

    【解决方案2】:

    请在 .h 中像这样添加 UISearchBarDelegate

    @interface CategoriesViewController : UIViewController <UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>{
    

    在 .m 中放入代码

    searchBar.delegate=self;
    

    【讨论】:

      【解决方案3】:

      您可以直接在 xib 的 tableview 中添加搜索栏,而不是编写这么多的代码,即 table 的子视图。否则,您可以将 uisearchbar 作为 .h 文件中的 searchBar ,您必须分配此 searchBar 并委托 searchbar 并使用 .h 文件中的所有 searchbar 方法。

      【讨论】:

      • 但我不想通过 IB 来做这件事……只能以编程方式进行
      • 多几行代码将有助于解决您的问题。
      • 行包含什么?处理搜索的方法?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 2020-09-14
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多