0

xcode 新手试图让 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
4

1 回答 1

0

你确定你有代表:在你的视图控制器 .h 文件中吗?此外,请确保您已从情节提要中选择“UISearchBar 和 UISearchDisplay 控制器”作为您的搜索栏对象(假设您正在使用情节提要)。确保您已将 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
 }
于 2013-03-20T17:08:36.920 回答