我有两个控制器:主要的一个是LocationItemTableViewController
包含所有图形元素和未过滤的表格;另一个是SearchResultsTableViewController
,用于处理搜索的结果。我希望搜索结果显示在LocationItemTableViewController
新表格中。
当搜索开始时updateSearchResultsForSearchController
在主控制器上调用,这会将搜索结果分配给resultsController
(的实例SearchResultsTableViewController
),它会在主视图控制器上显示它们。
问题:
什么时候
updateSearchResultsForSearchController
叫我NavigationBar
居然消失了!为什么?当
updateSearchResultsForSearchController
被调用时,结果会正确显示,但是如果我向上滚动它们,它们会在下方通过SearchBar
,然后它们会再次出现在应该有NavigationBar
..当我点击一个我已经实现的按钮时,
cellForRowAtIndexPath
它会给我这个警告:
警告:尝试在 LocationItemTableViewController: xxxxxx 上显示 UIAlertController: xxxxxxx,其视图不在窗口层次结构中!
该按钮应该调用一种操作表(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;
}