gpt4 book ai didi

ios - UISeachBar 和委托(delegate)方法

转载 作者:可可西里 更新时间:2023-11-01 05:35:48 24 4
gpt4 key购买 nike

我正在尝试在填充有 NSMutableArray 的 UITableView 上完成搜索栏。问题是搜索栏委托(delegate)方法显然不起作用。当我在搜索栏中输入内容时,我没有得到任何答案,甚至 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);
}

最佳答案

您还没有为 searchBar 对象设置委托(delegate)。在那行之后:

[searchBar setShowsScopeBar:YES];

添加:

searchBar.delegate = self;

不要忘记添加 <UISearchBarDelegate>协议(protocol)。

关于ios - UISeachBar 和委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20288411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com