Searchbar in tableView and reload data - ios

I can not search because my search bar is a header of tableView and then I reload tableView I have not search results.
I know there is a lot of ways to solve it but what is more wisely?
My code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.searchBar.delegate = self;
view.opaque = NO;
view.backgroundColor = [UIColor clearColor];
self.searchBar.opaque = NO;
self.searchBar.translucent = NO;
self.searchBar.backgroundColor = [UIColor clearColor];
[view addSubview: self.searchBar];
self.searchBar.barStyle = UISearchBarStyleDefault;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ;
self.searchBar.barTintColor = [UIColor clearColor];
return view;
}
And I want that then I scroll tableView my searchBar will disappear in top and then I scroll up it will appear. Is any simple to do it?

You need to use tableHeaderView and not section header. Just put in your viewDidLoad
UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.tableView.tableHeaderView = searchBar;

Related

searchBar‘s frame changed after clicked cancel button

I add my searchBar(left) and a button(right) as subViews to a UIView.Then set the UIView as the TableHeaderView.But after I end searching and click the cancel button back to the tableview,the searchBar will totally cover the tableHeaderView and can't see the button added.And I viewed the layer,the button still there,just covered by the expanding searchbar.Anyone know how to resolve this?Thanks in advance!
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WinWidth, 40)];
self.searchController.searchBar.frame = CGRectMake(0, 0, WinWidth-50, 40);
UIButton *filterBtn = [[UIButton alloc]initWithFrame:CGRectMake(WinWidth-30, 10, 20, 20)];
filterBtn setBackgroundImage:[UIImage imageNamed:#"arrow_expend_down"] forState:UIControlStateNormal];
[view addSubview:self.searchController.searchBar]; [self.searchController.searchBar addSubview:filterBtn];
self.searchController.searchBar.barTintColor = [self colorWithRGB:0xf2f2f2 alpha:1];
[view addSubview:filterBtn];
view.backgroundColor =self.searchController.searchBar.barTintColor; self.tableView.tableHeaderView = view;
try adding : tableView.tableHeaderView.contentMode = UIViewContentModeCenter;
Try this
[self.searchController.searchBar sizeToFit];

Add footer view UITableView

I have this code to add a footer view to my table view
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,80.0f)];
UILabel *tableFooter = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.tableView backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = #"test";
[footerView addSubview:tableFooter];
self.tableView.tableFooterView = footerView;
but then I when I run this I do not see the label at the bottom of my code, I am it to my view, and then set my view as to footer, I see the extra space but not the label?
Not sure why this is happening?
Thanks

stange behaviour UIRefreshControl with UISearchBar

i'm using UIRefreshControl with UISearchBar when i'm pull to refresh i see a white background at the top of uiviewcontroller
UISearchBar *searchBar =
[[UISearchBar alloc] initWithFrame:
CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:#selector(update) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:_refreshControl];
the problem is the white background above
any ideas?
Create a background view for the table and set its color to clearColor, like this
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}

UICollection view hiding UINavigationBar

Basically I am trying to set the background color of my UINavigationBar but it doesn't show anything whatsoever.
Please, how can I fix this ?! Thanks a Million !!
This is my code in viewDidLoad:
[super viewDidLoad];
self.filteredContentList = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.collectionView.frame), 30)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.placeholder = #"Search...";
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView addSubview:self.searchBar];
[self.view addSubview:self.searchBar];
[self.collectionView setContentOffset:CGPointMake(0, 44)];
[self.collectionView setContentInset:UIEdgeInsetsMake(80, 0, 0, 0)];
self.navigationController.navigationBar.backgroundColor = [UIColor greenColor];
This works fine with iOS 7, and you adapt to your code above:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[navBar setBarTintColor:[UIColor greenColor]];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(navBar.frame), 30)];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.placeholder = #"Search...";
[self.view addSubview:searchBar];
[self.view addSubview:navBar];

Adding subviews to tableHeaderView with UISearchBar in iOS7 misbehaves

How can one add two simple UILabels to a TableHeaderView and retain the default searchbar behavior of the search display controller in iOS7?
Or, visually:
Why does this code:
UIView *tableHeadView = self.tableView.tableHeaderView;
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, 320, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Questions"
UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = #"test test test test";
[tableHeadView addSubview:tableHeaderLabel];
[tableHeadView addSubview:tableHeaderPrononce];
added to a UITableViewController viewDidLoad event (which contains a UISearchDisplayerController)
gives this lovely result in iOS6:
and this horrible terrible result in iOS7:
The behavior:
During normal mode the UILabels I added are not shown. while search is active the UILabels suddenly appear ON TOP OF the table cells and does not scroll away
In addition I am getting crashes while searching in iOS 7 which never occurred on iOS6. Probably not related to this piece of code but nevertheless I should mentioned that.
I tried all I could find on SO about fixing this issue but always something else breaks or disappears (mainly the UISearchBar).
Help
Look dude, before you modify your code or get frustrated with the behavior of the iOS7, it is advised to go through the UI Transition Guide given by apple.
After reading this you will get to know, why the grouped style tableView is looking annoying is not at all annoying from ios7 point of view. Each group extends the full width of the screen.
Moreover the UISearchBar behavior can be controlled if you read through Search Bar and Scope Bar
I hope that helps. Should you need all the code, please let us know. we can provide sample.
Here is Sample Code as Required Adding UISearchBar to View rather than TableViewHeader
Adding UISearchBar to self.view
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)];
searchBar.delegate = self;
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
[self.view addSubview:searchBar];
Adding UITableView to self.view
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tableView];
Creating and Adding UITableView.tableHeaderView
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Questions";
tableHeaderLabel.alpha = 0.9;
UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = #"test test test test";
tableHeaderPrononce.alpha = 0.7;
UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[aHeaderView setBackgroundColor:[UIColor clearColor]];
[aHeaderView addSubview:tableHeaderLabel];
[aHeaderView addSubview:tableHeaderPrononce];
tableView.tableHeaderView = aHeaderView;
Here are the results:
SearchDisplayController
// Create search controller
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;
You can do this simply by UITableViewDelegate method shown below:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
sectionHeaderView.backgroundColor = [UIColor grayColor];
UILabel *headerLabel = [[UILabel alloc] init];
[sectionHeaderView addSubview:headerLabel];
return sectionHeaderView;
}

Resources