所以我试图在iOS7中将搜索栏和导航栏结合起来用于tableview。
我打过电话self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
它看起来很好。
但是,当我点击tableview中的任意位置时,搜索栏会被激活。我基本上无法定期点击表格单元格。
我想知道我到底错过了什么?
作为参考,与tableview相关的代码如下所示:(我使用storyboard处理点击逻辑)
#pragma mark - Table view delegate method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [self.contacts[section] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"contactCell";
UITableViewCell *cell;
Contact *contact;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
contact = searchResults[indexPath.row];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:7];
UIImageView *starMark = (UIImageView *)[cell viewWithTag:9];
NSString *nameString = [NSString stringWithFormat:@"%@ %@",contact.firstName, contact.lastName];
if ([contact.star isEqual:@0]) {
starMark.hidden = YES;
} else {
starMark.hidden = NO;
}
[nameLabel setText:nameString];
return cell;
}
这里定义了segue方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"pushContactDetail"]) {
NSIndexPath *indexPath;
Contact *contact;
if (self.searchDisplayController.active == YES) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
contact = searchResults[indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
//get indexPath from selected sushi
//initialize the detail view controller and push it
CIContactViewController *destViewController = segue.destinationViewController;
destViewController.contact = contact;
}
}