【发布时间】: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