2009-08-11 64 views
4

我的目标是在UITableView上方的视图中修复UISearchBar。当我在IB中进行设置然后构建时,表视图展开以填充整个窗口,并且搜索栏不可见。如果我将UISearchBar作为UITableView的子视图,则搜索栏将按预期显示,但这不是我想要的。我所追求的是,​​在用户选择一行后,我想显示一个详细视图,搜索栏仍然保留在屏幕上。所以我认为它需要成为一个单独的子视图,而不是表格的一部分。由于某些原因,我不能让搜索栏显示时,它只是表格外的子视图。无法获取UISearchBar,除非在表格视图中显示

回答

5

您必须以编程方式执行此操作。这是我最终做的。 首先将以下内容添加到ViewController的.h文件中。

@interface YourViewController : UITableViewController <UISearchBarDelegate> 
{ 
    UISearchBar *mySearchBar; 
} 
    @property (nonatomic, retain) UISearchBar *mySearchBar; 
    @end 

然后把这段代码放到我的RootViewController的viewDidLoad方法中。

self.mySearchBar = [[[UISearchBar alloc] 
      initWithFrame:CGRectMake(0.0, 64.0, self.view.bounds.size.width, 
      44.0)] autorelease]; 
    self.mySearchBar.delegate = self; 
    self.mySearchBar.showsCancelButton = YES; 
    self.mySearchBar.hidden = YES; 
    self.mySearchBar.tintColor = [UIColor lightGrayColor]; 
    [self.navigationController.view addSubview: self.mySearchBar]; 

您可能还需要添加这样的内容来防止searchBar位于您的tableView之上。

self.tableView.frame = CGRectMake(0.0, 44.0, 320, 324); 

对我而言,当我深入细节视图时,searchBar仍然保留在视图中。我不得不打电话:

self.mySearchBar.hidden = YES;` 

在我didSelectRowAtIndexPath来摆脱它,当我点击一个单元格。

+0

谢谢约拿。这让我需要去的地方! – skantner 2009-08-20 00:33:41

1

它不适用于UITableViewController。这里是什么会给你想要的行为:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

    return search; //in .h, IBOutlet UISearchBar* search; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

    return 44; 
} 
+0

Hi Poitr。需要更多关于如何适应上述代码的信息,或者它是否完全无法使用。我正在尝试这样做。谢谢。 你是在另一个控制器或在TVC中做这个吗? – 2012-06-26 15:04:30

+0

嗯,Piotr这只是把搜索栏放到标题中,如果你打算在标题中使用任何内容,这是不好的。 – 2012-06-26 15:18:53

+0

是的,而不是直接使用搜索视图,而是可以创建包含搜索和任何其他元素的UIView。 – 2012-09-09 08:19:18