xcode newbie试图让UISearchBar功能正常运行。
我正在使用故事板,我的tableview控制器在数组json的单元格中显示事件列表(标题,位置和开始日期)。当表第一次加载时,我的所有结果都显示正常。当我单击将一些文本放入搜索栏(标题中存在的文本)时,视图控制器中不会显示任何内容(即,它会过滤掉所有事件)。希望这更有意义。
我的头文件是
#import <UIKit/UIKit.h>
#define kGETUrl @"http://www.max-momentus.com/fetchevents.php5"
@interface MMAllEventsViewController : UITableViewController <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *json;
NSMutableArray *jsonFiltered;
BOOL isFiltered;
}
@property (weak, nonatomic) IBOutlet UISearchBar *eventSearchBar;
@property (strong, nonatomic) IBOutlet UITableView *eventTableView;
@property (strong, nonatomic) NSString *categorySelected;
@property (strong, nonatomic) NSString *displayDate;
@end
我的实施文件是
#import "MMAllEventsViewController.h"
#import "MMEventDetailViewController.h"
#import "MMCategoryViewController.h"
#import "MMEventCell.h"
@interface MMAllEventsViewController ()
@end
@implementation MMAllEventsViewController
@synthesize categorySelected, displayDate;
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"EventDetailSegue"]) {
// Segue code
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void) getData:(NSData *) data {
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self.tableView reloadData];
}
-(void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.eventSearchBar.delegate = self;
self.eventTableView.delegate = self;
self.eventTableView.dataSource = self;
[self start];
}
-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
isFiltered = NO;
}
else {
isFiltered = YES;
for (int i = 0; i < [json count]; i++)
{
NSMutableDictionary *temp = (NSMutableDictionary*) [json objectAtIndex:i];
NSString *name = [NSString stringWithFormat:@"%@", [temp valueForKey:@"Title"]];
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
[jsonFiltered addObject:name];
}
i++;
}
}
[self.eventTableView reloadData];
}
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.eventTableView resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isFiltered) {
return [jsonFiltered count];
}
return [json count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EventCell";
MMEventCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[MMEventCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (!isFiltered) {
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.eventTitleLabel.text = [info objectForKey:@"Title"];
cell.locationLabel.text = [info objectForKey:@"Location"];
NSString *startDate = [info objectForKey:@"StartDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *sdate = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:@"EEE, d MMM yy"];
NSString *convertedStartDate = [dateFormatter stringFromDate:sdate];
NSString *endDate = [info objectForKey:@"EndDate"];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *edate = [dateFormatter dateFromString:endDate];
[dateFormatter setDateFormat:@"EEE, d MMM yy"];
NSString *convertedEndDate = [dateFormatter stringFromDate:edate];
if([sdate isEqualToDate:edate]) {
displayDate = convertedStartDate;
} else {
displayDate = [convertedStartDate stringByAppendingString:@" to "];
displayDate = [displayDate stringByAppendingString:convertedEndDate];
}
cell.datesLabel.text = displayDate;
}
else {
NSDictionary *info = [jsonFiltered objectAtIndex:indexPath.row];
cell.eventTitleLabel.text = [info objectForKey:@"Title"];
cell.locationLabel.text = [info objectForKey:@"Location"];
NSString *startDate = [info objectForKey:@"StartDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *sdate = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:@"EEE, d MMM yy"];
NSString *convertedStartDate = [dateFormatter stringFromDate:sdate];
NSString *endDate = [info objectForKey:@"EndDate"];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *edate = [dateFormatter dateFromString:endDate];
[dateFormatter setDateFormat:@"EEE, d MMM yy"];
NSString *convertedEndDate = [dateFormatter stringFromDate:edate];
if([sdate isEqualToDate:edate]) {
displayDate = convertedStartDate;
} else {
displayDate = [convertedStartDate stringByAppendingString:@" to "];
displayDate = [displayDate stringByAppendingString:convertedEndDate];
}
cell.datesLabel.text = displayDate;
}
return cell;
}
@end
答案 0 :(得分:0)
您确定拥有代表:在您的视图控制器.h文件中?此外,请确保您已从故事板中选择“UISearchBar和UISearchDisplay Controller”作为搜索栏对象(假设您正在使用故事板)。确保您已将SearchBar链接到您的视图控制器作为其委托,并且它已连接到代码中的IBOutlet对象(再次假设您使用的故事板:
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
最后,假设您已声明了一个UISearchDisplayController变量,将其链接到 UITableView ,然后确保在加载搜索结果时检入
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
和
- (NSInteger)numberOfRowsInTableView:(UITableView *)tableView
如果:
if(tableView == searchDisplay):
{
...load results.
}
else
{
...load normal view
}