【问题标题】:Adding UISearchBar to UITableView将 UISearchBar 添加到 UITableView
【发布时间】:2014-02-27 09:20:13
【问题描述】:

背景: 我有一个 UIViewController,它以编程方式添加了一个 UITableView。在 UITableView 的顶部,我有一个 tablHeaderView,我在其中放置了一个 UIView。这个 UIView 有一个 UISearchBar 和一个 segmentedControl。想法是:用户可以按一些基本类别(例如“日期/时间”或“位置”等)对 UITableView 进行排序。他们还可以按程序中的项目进行搜索。

问题:当我点击搜索栏时,它会调整大小(我不想要),然后当我取消搜索时,它会再次调整大小并停留在那里,直到 UIViewController 退出并再次加载。

代码:

-(void)loadTableView
{
    usableSpace = [[UIScreen mainScreen] applicationFrame];
    usableWidth = usableSpace.size.width;
    usableHeight = usableSpace.size.height;

    _tableView = [[UITableView alloc] init];
    [_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    [self.view addSubview:_tableView];

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
    _searchBar.showsCancelButton = YES;

    NSLog(@"searchBar height = %fl", _searchBar.frame.size.height);
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Date/Time", @"Location", @"Speaker", nil]];
    segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
    [segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
    [headerView addSubview:_searchBar];
    [headerView addSubview:segmentedControl];
    _tableView.tableHeaderView = headerView;

    [self.view addSubview:_tableView];
    self.tableView = _tableView;

}

我尝试过的:我尝试过设置 SearchBar 的大小,而不是设置它的大小。没有它在 tableHeaderView 的 UIView 中,而只是让它自己存在。

为什么它会调整大小,我怎样才能让它停止?

编辑:我刚刚尝试了以下操作:在故事板(最初创建 UIViewController 的位置)中,我选择了有问题的 UIVC,在属性检查器中,我取消选择了“在顶部栏下”和“在底部栏,这似乎已经解决了动画问题的第一部分。现在,当我在搜索栏中点击时,搜索变为活动状态,但 searchBar 不会调整大小。但是,当我取消 searchBar 时,searchBar 仍会根据屏幕截图中的最后一张图像进行动画处理和调整大小。什么可能导致调整大小?

【问题讨论】:

  • 我有同样的问题,我尝试使用这个解决方案。它工作正常。 stackoverflow.com/questions/21419992/…>

标签: ios objective-c cocoa-touch uitableview


【解决方案1】:

我一直在努力解决这个问题太久了……支持this dude here for the clues

如果您希望您的 UISearchBar 与其他视图一起位于标题中并随着标题滚动而不是停留在顶部,那么您必须将其删除并在搜索完成后重新添加它。要清楚,像你已经做的那样建立你的标题,然后把它扔在那里以及处理扭曲的动画。

-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    [self.mySearchBar removeFromSuperview];
    self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
    [self.table.tableHeaderView addSubview:self.mySearchBar];
}

正如 Timothy Moose 所指出的 here,“UISearchDisplayController 似乎做了一个未记录的假设,即搜索栏是标题视图的唯一内容”

...耶苹果。

【讨论】:

    【解决方案2】:

    好的,经过数小时的阅读和尝试不同的事情,我想我已经找到了如何在我想要的地方拥有一个 UISearchBar 并且它似乎工作正常。我认为问题在于 tableHeaderView 中有多个视图,或者 UISearchBar 不喜欢在“包含”视图中使用另一个视图。

    这是我最终得到的代码,它允许我在 UITableView 的顶部有一个 segmentedControl 和一个 UISearchBar,它实际上位于我的 UIView 的顶部(添加了 UITableView)。

    -(void)loadTableView
    {
        usableSpace = [[UIScreen mainScreen] applicationFrame];
        usableWidth = usableSpace.size.width;
        usableHeight = usableSpace.size.height;
    
        _tableView = [[UITableView alloc] init];
        [_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [self.view addSubview:_tableView];
    
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
        [_searchBar setShowsCancelButton:YES];
    
        NSLog(@"searchBar height = %fl", _searchBar.frame.size.height);
        segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Title", @"Date/Time", @"Speaker", nil]];
        segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
        segmentedControl.selectedSegmentIndex = 0;
        [segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged];
    
        searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
        searchDisplayController.delegate = self;
        searchDisplayController.searchResultsDataSource = self;
        searchDisplayController.searchResultsDelegate = self;
    
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
        [headerView addSubview:segmentedControl];
        _tableView.tableHeaderView = headerView;
    
        [self.view addSubview:_tableView];
        [self.view addSubview:_searchBar];
        self.tableView = _tableView;
    }
    

    这意味着我有: UIViewController 带有一个 UINavigationController、UISearchBar、UITableView。 UITableView 有一个segmentedConrol。 UISearchBar 位于 UINavigationController 下方并且始终存在。 UISearchBar 位于 UITableViewController 的顶部,并与 UITableViewCells 一起滚动。

    如果有人知道为什么 UISearchBar 在问题中的行为如此,那么如果他们能发表评论,我将不胜感激。我知道很多人都遇到过这个问题(或非常相似的问题),但我在任何地方都没有找到明确的答案。

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多