UISearchController的问题。如何在新表上正确显示搜索结果

时间:2016-03-13 20:45:41

标签: ios objective-c iphone uisearchcontroller ios9.2

我有两个控制器:主控制器是LocationItemTableViewController并包含所有图形元素和未过滤的表格;另一个是SearchResultsTableViewController,用于处理搜索结果。我希望搜索结果显示在LocationItemTableViewController的新表格中。

当搜索开始时,在主控制器上调用updateSearchResultsForSearchController,并将搜索结果分配给resultsControllerSearchResultsTableViewController的实例),并在主视图控制器上显示它们。

问题:

  • updateSearchResultsForSearchController被调用时,NavigationBar意外消失!为什么?

  • 调用updateSearchResultsForSearchController时,结果会正确显示,但如果我向上滚动它们会在SearchBar下方传递,然后它们会再次出现在应该有NavigationBar的位置..

  • 当我点击我在cellForRowAtIndexPath中实施的按钮时,它会给我这个警告:

警告:尝试在LocationItemTableViewController上显示UIAlertController:xxxxxxx:xxxxxx,其视图不在窗口层次结构中!

该按钮应该在UIAlertController上调用一种操作表(LocationItemTableViewController)。

显然,我在做什么在概念上是错误的,但我无法理解是什么...... 有什么提示吗?谢谢!

LocationItemTableViewController.h

@interface LocationItemTableViewController : UIViewController <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UIAlertViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, NSCoding, UIActionSheetDelegate>
@property (strong,nonatomic) NSMutableArray *filteredLocationItemArray; //Filtered results array
@property (nonatomic, retain) IBOutlet UINavigationBar *NavigationBar;

LocationItemTableViewController.m

@interface LocationItemTableViewController ()
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (nonatomic, strong) SearchResultsTableViewController *resultsController;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchResults = [NSMutableArray arrayWithCapacity:[self.LocationItemArray count]];

    resultsController = [[SearchResultsTableViewController alloc] init];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:resultsController];
    self.searchController.searchResultsUpdater = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;

    self.searchController.searchBar.placeholder = nil;
    [self.searchController.searchBar sizeToFit];
    self.searchController.dimsBackgroundDuringPresentation = YES;
    self.searchController.searchBar.delegate = self;
    self.searchController.delegate = self;

    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y + NavigationBar.bounds.size.height +20,  self.tableView.frame.size.width, self.tableView.contentSize.height);
}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {

    NSString *searchString = [self.searchController.searchBar text];

    NSString *scope = nil;

    [self updateFilteredContentForProductName:searchString type:scope];

    if (self.searchController.searchResultsController) {

        resultsController.filteredEvents = self.searchResults;

        resultsController.tableView.frame = CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-ToolBar.bounds.size.height);

        //Now reload the table but this time containing the search results
        [resultsController.tableView reloadData];
    }
}

SearchResultsTableViewController.h

@interface SearchResultsTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *filteredEvents;

SearchResultsTableViewController.m

@interface SearchResultsTableViewController ()
@property (nonatomic, strong) LocationItemTableViewController *instanceOfLocationItemTableViewController;
@end

@implementation SearchResultsTableViewController
@synthesize instanceOfLocationItemTableViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Create instance of LocationItemTableViewController.h
    instanceOfLocationItemTableViewController = [[LocationItemTableViewController alloc] init];

    [self.tableView setAllowsSelection:NO];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.filteredEvents ? 1 : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"CALLED numberOfRowsInSection --: %lu", (unsigned long)[self.filteredEvents count]);

    return [self.filteredEvents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Create a new LocationItem Object
    LocationItem *locationItem = nil;
    locationItem = [self.filteredEvents objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"SearchResultCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    ....
    [[cell textLabel] setText:[locationItem name]];

    return cell;
}

0 个答案:

没有答案