PFQueryTableViewController (parse.com) behaves weirdly when UISearchBar is added as header. - ios

For some reason, my PFQueryTableViewController's table view behaves weirdly when I add a UISearchBar as the header. When added, the table view fails to load objects queried from the DB when the app starts up. It displays Loading without actually displaying any objects. When I pull-to-refresh however, it populates with the objects perfectly. Another problem is the cell dividers (thin gray lines) disappear. Here is the relevant code:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.parseClassName = #"userListing";
self.textKey = #"listingName";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 15;
self.tableView.rowHeight = 60;
self.locationForQuery = [[PFUser currentUser] objectForKey:#"userLocation"];
//append the add button and title to the navigation bar
UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addListing)];
[self.navigationItem setRightBarButtonItem:addButton];
self.navigationItem.title = #"Listings";
}
return self;
}
- (void)viewDidLoad{
self.locationSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.locationSearchBar.delegate = self;
self.tableView.tableHeaderView = locationSearchBar;
}
Let me know what else you need to see.

Figured out the problem. I forgot to call [super viewDidLoad] in my overwrite.

Related

UISearchController's SearchBar gets detached and overlaps TableView if rotated while not visible

So I've got a UITableView with a UISearchController. It loads up fine, then I do my search and select a result, pushing a new view controller to my UINavigationController.
If, while the new VC is onscreen, I rotate my phone, then rotate back, and go back...when I get back to the UITableView, the UISearchBar is nowhere to be seen. If, to troubleshoot, I hide the UINavigationBar, or if I look at the view hierarchy, I find that it has moved up behind the navigation bar and is being covered.
I've tried all kinds of hackey solutions, like moving the frame of the bar after it finishes loading, with no success. I really need help on this one, I'm stumped. None of the stuff I've found on google has helped.
Here are screenshots of before, after, and the view heirarchy:
Screenshots
Here is all the View-related code:
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.title)
self.title = #"GLOBAL SEARCH";
self.tableView.backgroundColor = [UIColor whiteColor];
//TESTING
//if I don't have this, the bar detaches from the navigation bar
self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = YES;
//test
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x,
self.searchController.searchBar.frame.origin.y,
self.searchController.searchBar.frame.size.width,
44.0);
self.searchController.searchBar.backgroundImage = [[UIImage alloc] init];
self.searchController.searchBar.backgroundColor = kConstantBaseColor;
self.searchController.searchBar.barTintColor = kConstantBaseColor;
self.searchController.searchBar.tintColor = kConstantLabelColor;
//set color
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:kConstantLabelColor}];
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.clipsToBounds = NO;
self.tableView.tableHeaderView.clipsToBounds = NO;
self.tableView.tableHeaderView.backgroundColor = kConstantBaseColor;
//put purple block above, in case user scrolls up
CGRect bufferFrame = CGRectMake(self.searchController.searchBar.frame.origin.x,
self.searchController.searchBar.frame.origin.y-300,
self.searchController.searchBar.frame.size.width*2,
300);
UIView *purpleBuffer = [[UIView alloc] initWithFrame:bufferFrame];
purpleBuffer.backgroundColor = kConstantBaseColor;
[self.tableView addSubview:purpleBuffer];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
}];
}
Does anyone have any solutions? I'm stumped and have been beating my head against it for days. Thank you so much.

tableHeaderView UISearchBar not showing

I have added a UISearchBar programatically to my UITableView, it was showing perfectly fine untill I decided to add an offset to my UITableView to hide the UISearchBar when the view is loaded. I would like help displaying it again.
This is what my code looks like.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.contentOffset = CGPointMake(0.0f, 44.0f);
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
mySearchBar.keyboardType = UIKeyboardTypeAlphabet;
mySearchBar.delegate = self;
self.tableView.tableHeaderView = mySearchBar;
// Create the search display controller
UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
I am not really sure where to go to from here.
This code will work in both iOS6 and iOS7.
Note that in iOS7 you will loose transparency of NavigationBar
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
[self.tableView setContentOffset:CGPointMake(0, mySearchBar.frame.size.height)];
If you want to save default transparency in iOS7 use this code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
[self.tableView setContentOffset:CGPointMake(0, -20)];
}
else {
[self.tableView setContentOffset:CGPointMake(0, mySearchBar.frame.size.height)];
}
}

UISearchBar inside UIPopoverController moving on click

I'm currently trying to place a table view and search bar inside a popover but I'm getting a really weird bug. Whenever I click on the search bar, the cancel button animates in and the bar promptly lowers about the size of a status bar.
I've tried playing around with the UIBarPosition delegate, but that doesn't do anything either. I've tried just about everything I can think of so I thought I'd ask your help. Here's the code I use in the UITableViewController to add the search bar to the table header:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self)
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, popoverWidth, singleRowHeight)];
searchBar.delegate = self;
searchBar.showsScopeBar= YES;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
searchDisplayController.searchResultsTableView.rowHeight = singleRowHeight;
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView.tableHeaderView = searchBar;
return self;
}
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTop;
}
Thanks!
according to Extra space above search bar when UISearchDisplayController is active
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout:)]) { /// iOS 7 or above
self.edgesForExtendedLayout = UIRectEdgeNone;
}
}

MasterViewController's TableViewController null in constructor?

I have the following MasterViewController:
- (id) init{
self = [super init];
if(self){
//self.title = #"Main Menu";
//self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"view-1_0000s_0000_Muskoka-Logo1"]];
self.navigationItem.titleView = imageView;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"view-1_0002s_0003s_0001_Bottom-panel"]];
self.tableView.scrollEnabled = NO;
}
return self;
}
It crashes when I try to set the background colour of the tableviewcontroller because the tableview inside the master is null. The image there is not null of course.
My only guess is that [super init] is not working properly?
A view controller's views and subviews do not exist until they have been loaded, which will occur during loadView (where the view hierarchy is built either in code or from a xib) All of those customisations should be in viewDidLoad. I'm surprised it's crashing, though, messages to nil don't cause crashes.

Creating a UISearchDisplayController programmatically

I'm trying to create a UISearchDisplayController programmatically. I have a method which should set up my search controller, but when I call it, nothing happens.
This my -setupSearch method:
- (void)setupSearch {
UISearchBar *myBar;
UISearchDisplayController *myCon;
myBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[myBar sizeToFit];
myCon = [[UISearchDisplayController alloc]
initWithSearchBar:myBar contentsController:self];
[myBar release];
myCon.delegate = self;
myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
/* Setup scopes */
{
NSMutableArray *scopes;
NSUInteger count, i;
NSString *aScope;
count = SCOPE_COUNT;
scopes = [[NSMutableArray alloc] initWithCapacity:count];
for(i = 0; i < count; i++) {
// I create four scopes here
}
myCon.searchBar.scopeButtonTitles = scopes;
[scopes release];
}
[myCon release];
}
I call the above method in the -viewDidLoad method of my subclassed UITableViewController. Unfortunately nothing happens when my table view controller get's displayed in a UITabBarController.
Any help would be greatly appreciated.
Check out the example code in:
[https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]
Repo here: https://github.com/JayMarshal/Grabcasts
It is an expanded version of the coredatatableviewcontroller of the stanford iOS courses.
Relevant snippet of that code follows:
- (void)createSearchBar {
if (self.searchKey.length) {
if (self.tableView && !self.tableView.tableHeaderView) {
UISearchBar *searchBar = [[[UISearchBar alloc] init] autorelease];
self.searchDisplayController
= [[UISearchDisplayController alloc] initWithSearchBar:searchBar
contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
searchBar.frame = CGRectMake(0, 0, 0, 38);
self.tableView.tableHeaderView = searchBar;
}
} else {
self.tableView.tableHeaderView = nil;
}
Basically it attaches the UISearchDisplayController to self (which must be a tableviewcontroller) as a side effect of the initialization. So setting:
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
Instead of
myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
Might do the trick. In debugging, check whether myCon and self.searchDisplayController are pointing to the same object?
Updated: there seems to be a bug in the SDC property of the TVC that it is not retained in the runloop. Filed as: http://openradar.appspot.com/10254897 also mentioned on SO, see
UIViewController does not retain its programmatically-created UISearchDisplayController

Resources