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

时间:2015-02-27 09:15:26

标签: ios swift uisearchbar uisearchdisplaycontroller

我已经实现了一个空的ViewController,即带有SearchBar的SearchViewController。我正在从Web服务中搜索Ans,我希望仅在用户按下搜索按钮时才显示搜索结果。这已经实施。问题是,结果以一种奇怪的方式出现,如下所示:

enter image description here

不知道他们隐藏了什么。我如何将它们带到前面?

这是我的代码:

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无法弄清楚它是什么..请帮忙

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

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

编辑:

还尝试将搜索栏添加到searchTableView标题。

以下是将搜索栏添加到tableView标题的objective-c代码。

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;

答案 2 :(得分:1)

只是发布答案,有人会像我一样结束。

我没有将tableViewSearchDisplayController连接起来。

tableView应该是dataSource的{​​{1}}和Delegate

我们只需SearchDisplayController即可连接。

PS。在XCODE 6.1中,control+DragSearchDisplayController的标题中显示为类似于按钮的按钮。

答案 3 :(得分:0)

#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