Creating a UISearchDisplayController programmatically - ios

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

Related

How to add headerView like Navigationbar on ASViewController with ASTableNode without NavigationController?

In my application, I do not use the NavigationController, in general.
In UITableView I would do so, would add
[Self.view addSubview: self.myCustomHeaderView];
But I'm using ASTableNode. If I do something, something similar here:
- (instancetype)init {
_tableNode = [[ASTableNode alloc] init];
self = [super initWithNode:_tableNode];
if (self) {
_tableNode.dataSource = self;
_tableNode.delegate = self;
self.navigationItem = //navigationItem implement
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
navbar.backgroundColor = [UIColor redColor];
navbar.items = #[self.navigationItem];
ASDisplayNode *navBarNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView * _Nonnull {
return navbar;
}];
[self.node addSubnode:navBarNode];
}
return self;
}
Then this node is added as part of the list and scrolled along with the list, and I need it to be as fixed header.
initWith ASDisplayNode rather than ASTableNode,then add table node on VC's node,for example:
#interface OCTextureTableController : ASViewController<ASDisplayNode *>
#property (nonatomic, strong) ASTableNode *tableNode;
- (instancetype)init
{
self = [super initWithNode:[ASDisplayNode new]];
if (self) {
self.tableNode = [[ASTableNode alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableNode.backgroundColor = [UIColor yellowColor];
self.tableNode.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-NAVIGATION_BAR_HEIGHT);
[self.node addSubnode:self.tableNode];
}
(i) take Class A as base Class.
(ii) take Class B as derived Class having type of class A.
Now in class A Add a uiview on top. that will be considered as navigation.(do anything in that view)
To use same navigation bar: you have to take other all classes as having type of class A.
that's it.

custom UIActivityViewController

I want create custom UIActivityViewController. I create it and it works good. I have some elements in it and its ok. Now I place they in one line. How can I create UIActivityViewController with 2 lines of elements.
Now my code is:
- (id)init
if (self = [super init])
{
FacebookActivity *facebookActivity = [[FacebookActivity alloc]init];
TwitterActivity *twitterActivity = [[TwitterActivity alloc]init];
EmailActivity *emailActivity = [[EmailActivity alloc]init];
OpenInSafariActivity *openInSafari = [[OpenInSafariActivity alloc]init];
OpenInWebActivity *openInWeb = [[OpenInWebActivity alloc]init];
CopyLink *copyLink = [[CopyLink alloc]init];
copyLink.delegate = self;
facebookActivity.delegate = self;
twitterActivity.delegate = self;
emailActivity.delegate = self;
openInWeb.delegate = self;
openInSafari.delegate = self;
self = [self initWithActivityItems:#[emailActivity] applicationActivities:#[openInWeb, openInSafari, facebookActivity, twitterActivity, emailActivity, copyLink]];
self.view.backgroundColor = [UIColor redColor];
self.completionHandler = ^(NSString *activityType, BOOL completed)
{
};
}
return self;
And the second question is how can I set color of bottom view with all UIActivityViewController’s elements.
In the implementation of your custom UIActivity classes you need to implement the following class method:
+ (UIActivityCategory)activityCategory {
return UIActivityCategoryAction; // or return UIActivityCategoryShare
}
The return value defines which row the activity will appear on in the activity view. UIActivityCategoryShare puts the activity on the 1st row and UIActivityCategoryAction puts it on the 2nd row.
You can check the sample Code :https://github.com/ArpitLokwani/ALCustomActivityView

iOS 7 UITableView inside a popover

I have a UITableView which has some custom styling. This table view appears in two places in the app, one of which is inside a UIPopoverController. However when the tableview is inside the popover it takes on the default tableview styling as stated in the UI Transition Guide under "Popover".
The problem I have is that there appears to be nowhere to change this behaviour. Regardless of where I try and modify properties of the tableview the view inside the popover doesn't change.
Anyone dealt with this issue before or have any ideas?
Here is the init method of LibraryProductView where I create the table view:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.sectionOrdering = [NSArray arrayWithObjects:
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DESCRIPTION],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DOCUMENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_ACTIVE_INGREDIENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_PRODUCTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_DOCUMENTS], nil];
self.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectInset(self.bounds, 10, 0) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.showsVerticalScrollIndicator = NO;
[self addSubview:self.tableView];
}
return self;
}
Here is where the containing view (LibraryProductView) is added to the popover:
- (IBAction)didTouchInformationButton:(id)sender
{
if (_infoPopover != nil && _infoPopover.isPopoverVisible)
{
[_infoPopover dismissPopoverAnimated:YES];
return;
}
CGSize preferredSize = CGSizeMake(600.0f, 500.0f);
LibraryProductViewController* productController = [[[LibraryProductViewController alloc] initWithPreferredSize:preferredSize] autorelease];
productController.filterByMyCompany = NO;
productController.product = _activityInput.product;
UINavigationController* nav = [[[UINavigationController alloc] initWithRootViewController:productController] autorelease];
nav.title = _activityInput.product.name;
RELEASE(_infoPopover);
_infoPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
_infoPopover.popoverContentSize = CGSizeMake(preferredSize.width, preferredSize.height + 46);
[_infoPopover presentPopoverFromRect:_infoButton.frame inView:_infoButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
The LibraryProductView is created within viewDidLoad method of LibraryProductViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.libraryProductView = [[LibraryProductView alloc] initWithFrame:(usingPreferredSize ? CGRectMake(0.0, 0.0, preferredSize.width, preferredSize.height) : self.view.bounds)];
self.libraryProductView.dataSource = self;
self.libraryProductView.delegate = self;
[self.view addSubview:self.libraryProductView];
}
To set properties for the TableView you might do so in
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor redcolor]];
[tableView setSeparatorColor: [UIColor blueColor]];
return 1;
}
This, of course, assumes you have set UITableViewDataSource in your .h file

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

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

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.

Resources