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

Ios UISearchBar:隐藏在搜索栏后面的搜索结果,ios,swift,uisearchbar,uisearchdisplaycontroller,Ios,Swift,Uisearchbar,Uisearchdisplaycontroller,我实现了一个空的ViewController,即带有搜索栏的SearchViewController。Ans当我从web服务搜索时,我希望搜索结果仅在用户按下搜索按钮时显示。这已得到实施。问题是,结果以一种奇怪的方式出现,如下所示: 不知道他们隐藏了什么。我怎样才能把它们带到最前面 这是我的密码: override func viewDidLoad() { super.viewDidLoad() self.api.delegate = self activateSea

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

不知道他们隐藏了什么。我怎样才能把它们带到最前面

这是我的密码:

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.
}

我可能会做些傻事。’我想不出是什么。。请帮助

看起来您的搜索栏已放置在您的表视图上方。尝试在情节提要中缩小表格视图的比例,使表格视图的顶部位于搜索栏元素的下方。结果应正确显示

如果您在ViewWillDisplay方法中更改searchTableView框架,则当您在同一视图控制器中时,该方法将不会被调用

尝试在searchBarSearchButtonClicked方法内更改searchTableView框架

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

编辑:

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

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标题的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;

如果有人遇到像我这样的情况,就把答案贴出来

我没有将
tableView
连接到
SearchDisplayController

#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
tableView
应该是
SearchDisplayController
数据源
委托

我们只需要
控制+拖动
即可连接

PS.在XCODE 6.1中,
SearchDisplayController
ViewController

\import>的标题中显示为类似按钮的东西
#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
@接口TableViewController:UITableViewController @结束 #导入“TableViewController.h” @接口TableViewController(){ NSInteger_行; } @属性(弱、非原子)IBUISearchBar*搜索栏; @结束 @TableViewController的实现 -(id)initWithStyle:(UITableViewStyle)样式 { self=[super initWithStyle:style]; 如果(自我){ //自定义初始化 } 回归自我; } -(无效)viewDidLoad { [超级视图下载]; _行数=3; //[自我隐藏搜索栏]; } -(无效)视图将显示:(BOOL)动画{ [超级视图将显示:动画]; [self.tableView setContentOffset:CGPointMake(0,44)动画:否]; //self.tableView.tableHeaderView=self.searchBar; } -(无效)视图消失:(BOOL)已设置动画{ //self.tableView.tableHeaderView=nil; //[self.tableView.tableHeaderView removeFromSuperview]; [self.tableView setContentInset:UIEdgeInsetsMake(-0.3,0,0,0)]; [超级视图显示:动画]; } -(无效)隐藏搜索栏{ //隐藏搜索栏 [self.tableView setContentOffset:CGPointMake(0,44)动画:否]; } -(iAction)切换计数:(UIBarButtonim*)发送方{ 如果(_行==20){ _行数=3; }否则{ _行数=20; } [self.tableView重载数据]; } -(iAction)hideBar:(UIBarButtonItem*)发送方{ [自我隐藏搜索栏]; } #pragma标记-表视图数据源 -(NSInteger)表格视图中的节数:(UITableView*)表格视图 { //返回节数。 返回1; } -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节 { //返回节中的行数。 返回_行; } -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 静态NSString*CellIdentifier=@“Cell”; UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //配置单元格。。。 cell.textlab.text=@“cell”; 返回单元; } /* //替代以支持表视图的条件编辑。 -(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath { //如果不希望指定的项可编辑,则返回“否”。 返回YES; } */ /* //替代以支持编辑表格视图。 -(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径 { 如果(editingStyle==UITableViewCellEditingStyleDelete){ //从数据源中删除该行 [tableView deleteRowsAtIndexPaths:@[indexPath]和RowAnimation:UITableViewRowAnimationFade]; } else if(editingStyle==UITableViewCellEditingStyleInsert){ //创建相应类的新实例,将其插入数组,并向表视图添加新行 } } */ /* //替代以支持重新排列表视图。 -(void)tableView:(UITableView*)tableView移动rowatinexpath:(nsindepath*)从indepath到indepath:(nsindepath*)到indepath { } */ /* //重写以支持表视图的条件重新排列。 -(BOOL)tableView:(UITableView*)tableView可以移动rowatinexpath:(nsindepath*)indepath { //如果不希望该项目可重新订购,则返回“否”。 返回YES; } */ /* #pragma标记-导航 //在基于故事板的应用程序中,您通常需要在导航之前做一些准备 -(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方 { //使用[segue destinationViewController]获取新的视图控制器。 //将选定对象传递给新的视图控制器。 } */ @结束
为什么要将其标记为-1??4.我真的被卡住了,只好求助。。我删除了viewWillAppear方法..bt仍然无效:(如sharvan所示,将搜索栏视图添加为tableview标题的自定义标题视图问题可以在泰国bcz解决,搜索栏显示在表格标题中,表格标题始终位于结果表格的顶部。尝试将搜索栏放置在所有可能的位置。没有任何帮助:(