-2

I have implemented an empty ViewController i.e SearchViewController with a SearchBar in it. Ans as i am searching from a web service, i want the search results to be displayed only when the user presses the search button. That has been implemented. Bt the problem is, the results appear in a weird manner as shown below:

enter image description here

Dont know what are they getting hidden. How do i bring them to front??

Here is my code:

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

I might hv done something silly. bt m not able to figure out what is it.. pls help

Swapna Lekshmanan
  • 514
  • 10
  • 30

4 Answers4

1

It looks like your search bar has been placed over your table view. Try to scale your table view down in the storyboard so the top of the table view is below the search bar element. The results should display correctly

Vincent Rolea
  • 1,601
  • 1
  • 15
  • 26
1

You are changing the searchTableView frame inside the viewWillAppear method which will not get call when you are in the same view controller.

Try changing the searchTableView frame inside the searchBarSearchButtonClicked method.

Hope this will solve your problem. :)

Edit:

Also try adding the search bar to the searchTableView header.

Below is the objective-c code for adding the search bar to the 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;
i 4322946
  • 173
  • 2
  • 9
  • it didnt help.. i removed the viewWillAppear method..bt still no effect :( – Swapna Lekshmanan Feb 27 '15 at 10:47
  • as sharvan given here add search bar view as custom header view of tableview header problem may be resolved bcz in thais case search bar displayed in table header and table header is always on the top of resulted table . – user1548843 Feb 27 '15 at 11:36
1

Just posting the answer incase someone ends in a situation like mine.

I didn't connect the tableView to the SearchDisplayController.

The tableView should be the dataSource and Delegate for the SearchDisplayController.

We just need to control+Drag to connect.

PS. in XCODE 6.1 the SearchDisplayController is displayed as a button like thing in the header of ViewController.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Swapna Lekshmanan
  • 514
  • 10
  • 30
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
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36