【问题标题】:UISeachBar and delegate methodsUISeachBar 和委托方法
【发布时间】:2013-11-29 14:54:26
【问题描述】:

我正在尝试在填充有 NSMutableArray 的 UITableView 上完成搜索栏。问题是搜索栏委托方法显然不起作用。当我在搜索栏中输入内容时,我没有得到任何答案,甚至 NSLog(@"test") 也没有触发,这让我发疯了。如果我能得到任何帮助,请。 (这里是菜鸟ObjC级别^^)

这是我的代码(减去所有解析 xml 部分): (.h)

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MetamorphViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>{

    IBOutlet UITableView * newsTable;

    UISearchBar *searchBar;

    UISearchDisplayController *searchDisplayController;

    UIActivityIndicatorView * activityIndicator;

    CGSize cellSize;

    NSXMLParser * rssParser;

    NSMutableArray * stories;

    NSArray * newStories;

    NSMutableArray *filteredStories;

    NSString *dataType;

    NSString *idName;

    BOOL *uploaded;

    BOOL isFiltered;

    NSMutableDictionary * item;

    NSString * currentElement;
    NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink, * currentModule, *currentOwner, *currentOwnername;

}
- (void)parseXMLFileAtURL:(NSString *)URL typeOfModule:(NSString *)typeOfData;
@end 

(.m)

- (void)viewDidLoad {
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,44)];
    [searchBar setShowsScopeBar:YES];
    [searchBar setScopeButtonTitles:[[NSArray alloc] initWithObjects:@"OBJ",@"C", nil]];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    [searchDisplayController setSearchResultsDelegate:self];
    self.tableView.tableHeaderView = searchBar;
    [self.tableView reloadData];
    self.tableView.scrollEnabled = YES;

}


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSLog(@"test");
    //NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchBar];
     //newStories = [stories filteredArrayUsingPredicate:filterPredicate];
    //NSLog(@"newStories %@", newStories);
}

【问题讨论】:

  • 谢谢。效果很好。

标签: ios objective-c uitableview uisearchbar


【解决方案1】:

您尚未将委托设置为您的 searchBar 对象。在那一行之后:

[searchBar setShowsScopeBar:YES];

添加:

searchBar.delegate = self;

别忘了添加&lt;UISearchBarDelegate&gt; 协议。

【讨论】:

    【解决方案2】:

    您是否可能忘记为searchBar 设置委托。我不完全确定,但我认为searchDisplayController.delegate = self; 不会。

    【讨论】:

      【解决方案3】:

      您不需要成为 UISearchBar 的代表;名为searchDisplayController:shouldReloadTableForSearchString: 的 UISearchDisplayDelegate 方法是您要实现的方法。在这个方法中你需要做的是根据搜索字符串过滤你的数据,然后返回 YES 以重新加载 tableview。 UISearchDisplayDelegates 还可以接收用于将/已结束/开始搜索的回调,其中一些方法可能也需要实现。例如,撤消您在searchDisplayControllerWillEndSearch: 中的数据过滤,以便在取消搜索后重新显示您的所有数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 2016-11-10
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 2015-01-30
        • 1970-01-01
        相关资源
        最近更新 更多