UISearchBar:隐藏在搜索栏后面的搜索结果

UISearchBar: search results hidden behind the searchbar

我实现了一个空的 ViewController,即 SearchViewController,里面有一个 SearchBar。 Ans 当我从网络服务搜索时,我希望仅当用户按下搜索按钮时才显示搜索结果。那已经实施了。但问题是,结果以一种奇怪的方式出现,如下所示:

不知道他们在隐藏什么。我如何将它们放在前面?

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    self.api.delegate = self
    activateSearch()
    searchTableView.delegate = self
    searchTableView.dataSource = self
    searchBar.delegate = self
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
    var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary

    cell.textLabel?.text = rowData["title"] as? String

    return cell
}



func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

func didReceiveAPIResults(results: NSDictionary) {
    var resultsArr: NSArray = results["posts"] as NSArray
    dispatch_async(dispatch_get_main_queue(), {
        self.tableData = resultsArr
        self.searchTableView!.reloadData()
    })
}


func activateSearch() {
  // self.navigationController?.navigationBarHidden = true
    searchTableView.scrollRectToVisible(CGRectMake(0, 0, 1, 1), animated: false)
    searchBar.becomeFirstResponder()
}

    override func viewWillAppear(animated: Bool) {
    var newBounds:CGRect  = self.searchTableView.bounds;
    newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
    self.searchTableView.bounds = newBounds;
        self.navigationController?.navigationBarHidden = true
}

func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{

  api.searchItunesFor(searchBar.text)
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    self.viewWillAppear(true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

我可能做了一些傻事。 bt m 无法弄清楚它是什么.. 请帮助

您的搜索栏似乎已放置在您的 table 视图上。尝试在故事板中缩小 table 视图,使 table 视图的顶部位于搜索栏元素下方。结果应该正确显示

您正在更改 viewWillAppear 方法中的 searchTableView 框架,当您在同一个视图控制器中时,该方法将不会被调用。

尝试更改 searchBarSearchButtonClicked 方法中的 searchTableView 框架。

希望这能解决您的问题。 :)

编辑:

也尝试将搜索栏添加到 searchTableView header。

下面是 objective-c 代码,用于将搜索栏添加到 tableView header。

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)] ;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.keyboardType = UIKeyboardTypeAlphabet;
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;

只是发布答案,以防有人遇到像我这样的情况。

我没有将 tableView 连接到 SearchDisplayController

tableView 应该是 dataSourceDelegate 应该是 SearchDisplayController

我们只需要 control+Drag 即可连接。

PS。在 XCODE 6.1 中,SearchDisplayControllerViewController 的 header 中显示为类似按钮的东西。

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@end

#import "TableViewController.h"

@interface TableViewController () {
    NSInteger _rows;
}

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _rows = 3;

   // [self hideSearchBar];
}

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];

     [self.tableView setContentOffset:CGPointMake(0,44) animated:NO];
    // self.tableView.tableHeaderView = self.searchBar;


}

-(void)viewDidDisappear:(BOOL)animated{

      //self.tableView.tableHeaderView = nil;
    //[self.tableView.tableHeaderView removeFromSuperview];
    [self.tableView setContentInset:UIEdgeInsetsMake(-0.3, 0, 0, 0)];

    [super viewDidAppear:animated];
}

- (void)hideSearchBar {
    // hide search bar
    [self.tableView setContentOffset:CGPointMake(0,44) animated:NO];
}

- (IBAction)toggleCount:(UIBarButtonItem *)sender {
    if (_rows == 20) {
        _rows = 3;
    } else {
        _rows = 20;
    }
    [self.tableView reloadData];
}

- (IBAction)hideBar:(UIBarButtonItem *)sender {
    [self hideSearchBar];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = @"cell";

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

 @end