在UIViewController中添加搜索显示控制器

时间:2014-03-13 14:17:53

标签: ios objective-c uiviewcontroller uisearchbar uisearchdisplaycontroller

我想在UISearchDisplayController中实施UIViewController,因此没有实施UITableView。我创建了UISearchBarUISearchDisplayController并实现了相应的委托方法。

当我运行应用程序并尝试搜索时,不会显示应与搜索结果一起显示的表格视图。为了解释更多我的应用UI,UIViewController有一张地图,搜索栏放在导航控制器中。

我现在理解的是,似乎我应该实现一个表格视图,以便重复用于搜索结果。但是,没有地方/需要放置表格视图。我该怎么做才能解决这个问题?任何提示?

以下是我实现所有内容的代码:

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.

_search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[_search setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[_search setPlaceholder:@"Search"];
self.navigationItem.titleView = _search;

_searchArray = [[NSMutableArray alloc] initWithObjects:@"Hi", nil];

_searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:_search contentsController:self];
_searchDisplay.delegate = self;
_searchDisplay.searchResultsDataSource = self;
_searchDisplay.searchResultsDelegate = self;
[_searchDisplay setActive:YES animated:YES];

filteredResults = [[NSMutableArray alloc] init];}



 -(void) filterForSearchText:(NSString *) text scope:(NSString *) scope
{
    [filteredResults removeAllObjects]; // clearing filter array
    NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",text]; // Creating filter condition
    filteredResults = [NSMutableArray arrayWithArray:[_searchArray filteredArrayUsingPredicate:filterPredicate]]; // filtering result
    NSLog(@"search %@", filteredResults);
}

    #pragma mark - UISearchDisplayDelegate Methods
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterForSearchText:searchString scope:[[[[self searchDisplayController] searchBar] scopeButtonTitles] objectAtIndex:[[[self searchDisplayController] searchBar] selectedScopeButtonIndex] ]];

    return YES;
}

-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    return YES;
}

1 个答案:

答案 0 :(得分:-1)

#import <UIKit/UIKit.h>
#import "RoleDetailTVC.h" // so this class can be an RoleDetailTVCDelegate
#import "CoreDataTableViewController.h" // so we can fetch
#import "Role.h"

@interface RolesTVC : CoreDataTableViewController <RoleDetailTVCDelegate>

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) Role *selectedRole;
@property (nonatomic, retain) NSMutableArray *searchResults;

@end



#import "RolesTVC.h"
#import "Role.h"
#import "ModelCell.h"
#import <QuartzCore/QuartzCore.h>

@interface RolesTVC ()
{
    NSMutableArray *objects;

}

@end

@implementation RolesTVC
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize selectedRole;
@synthesize searchResults;


-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    objects = [[NSMutableArray alloc]init];

    if(self){
        for (int i=1; i<100; i++) {
            NSString *str = [NSString stringWithFormat:@"This is the fabulous Row %d",i];
            [objects addObject:str];
        }
    }
    return self;
}



//This function is where all the magic happens
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{


    //1. Setup the CATransform3D structure
    CATransform3D rotation;
    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
    rotation.m34 = 1.0/ -600;


    //2. Define the initial state (Before the animation)
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;

    cell.layer.transform = rotation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);

    //!!!FIX for issue #1 Cell position wrong------------
    if(cell.layer.position.x != 0){
        cell.layer.position = CGPointMake(0, cell.layer.position.y);
    }

    //4. Define the final state (After the animation) and commit the animation
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}


//Helper function to get a random float
- (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber {
    float diff = bigNumber - smallNumber;
    return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}

- (UIColor*)colorFromIndex:(int)index{
    UIColor *color;

    //Purple
    if(index % 3 == 0){
        color = [UIColor colorWithRed:0.93 green:0.01 blue:0.55 alpha:1.0];
        //Blue
    }else if(index % 3 == 1){
        color = [UIColor colorWithRed:0.00 green:0.68 blue:0.94 alpha:1.0];
        //Blk
    }else if(index % 3 == 2){
        color = [UIColor blackColor];
    }
    else if(index % 3 == 3){
        color = [UIColor colorWithRed:0.00 green:1.0 blue:0.00 alpha:1.0];
    }


    return color;

}

#pragma mark -
#pragma mark Fetched Results Controller section
- (void)setupFetchedResultsController
{
    // 1 - Decide what Entity you want
    NSString *entityName = @"Role"; // Put your entity name here
    //NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

    // 2 - Request that Entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
    [request setFetchLimit:100];
    // 3 - Filter it if you want
    //request.predicate = [NSPredicate predicateWithFormat:@"Role.name = Blah"];

    // 4 - Sort it if you want
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                                     ascending:YES
                                                                                      selector:@selector(localizedCaseInsensitiveCompare:)]];
    // 5 - Fetch it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
    [self performFetch];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupFetchedResultsController];
}

/**
 *  iCloud
 *
 *  @param  note    Check PersonTVC
 */
- (void)reloadFetchedResults:(NSNotification*)note {
    //NSLog(@"Underlying data changed ... refreshing!");
    [self performFetch];
}
- (void)viewDidLoad
{

    [ADVThemeManager customizeTableView:self.tableView];

    [self.tableView reloadData];

    /*
     * Refresh this view whenever data changes iCloud
     */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadFetchedResults:)
                                                 name:@"RolesTVCSomethingChanged"
                                               object:[[UIApplication sharedApplication] delegate]];


}

- (void)viewDidUnload
{

    /**
     *  iCloud
     *
     */
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    /**
     *  Search Function
    */
    self.searchResults = nil;
}

#pragma mark -
#pragma mark - Table view delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RolesCell";

    ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ModelCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }



    // Configure the cell...
    Role *role = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.nameLbl.text = role.name;
    cell.zoneLbl.text = role.zones;
    cell.checkoutLbl.text = role.checkout;

    //Set the accessory type.
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    //Configure the cell font
    cell.zoneLbl.font = [UIFont fontWithName:@"eurostile-oblique" size:18.0];
    cell.nameLbl.font = [UIFont fontWithName:@"eurostile-oblique" size:18.0];
    cell.checkoutLbl.font = [UIFont fontWithName:@"eurostile-oblique" size:18.0];

    return cell;
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResults count];
    }
    else
    {
        return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75.f;
}


#pragma mark -
#pragma mark Table view Methods
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.tableView beginUpdates]; // Avoid  NSInternalInconsistencyException

        // Delete the role object that was swiped
        Role *roleToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        //NSLog(@"Deleting (%@)", roleToDelete.name);
        [self.managedObjectContext deleteObject:roleToDelete];
        [self.managedObjectContext save:nil];

        // Delete the (now empty) row on the table
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self performFetch];

        [self.tableView endUpdates];


        NSError *error = nil;
        if (![[self fetchedResultsController] performFetch:&error]) {
            NSLog(@"Error! %@",error);
            abort();
        }

    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Add Role Segue"])
    {
        // NSLog(@"Setting RolesTVC as a delegate of RoleDetailTVC");
        RoleDetailTVC *roleDetailTVC = segue.destinationViewController;
        roleDetailTVC.delegate = self;

        // NSLog(@"Creating a new role and passing it to RoleDetailTVC");
        Role *newRole = [NSEntityDescription insertNewObjectForEntityForName:@"Role"
                                                      inManagedObjectContext:self.managedObjectContext];

        roleDetailTVC.role = newRole;
        // Hide bottom tab bar in the detail view
        roleDetailTVC.hidesBottomBarWhenPushed = YES;
    }
    else if ([segue.identifier isEqualToString:@"Role Detail Segue"])
    {
        // NSLog(@"Setting RolesTVC as a delegate of RoleDetailTVC");
        RoleDetailTVC *roleDetailTVC = segue.destinationViewController;
        roleDetailTVC.delegate = self;

        // Store selected Role in selectedRole property
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        self.selectedRole = [self.fetchedResultsController objectAtIndexPath:indexPath];

        // NSLog(@"Passing selected role (%@) to RoleDetailTVC", self.selectedRole.name);
        roleDetailTVC.role = self.selectedRole;
        // Hide bottom tab bar in the detail view
        roleDetailTVC.hidesBottomBarWhenPushed = YES;
    }
    else {
       // NSLog(@"Unidentified Segue Attempted!");
    }
}

- (void)theSaveButtonOnTheRoleDetailTVCWasTapped:(RoleDetailTVC *)controller
{
    // do something here like refreshing the table or whatever

    // close the delegated view
    [controller.navigationController popViewControllerAnimated:YES];    
}

// Override to support conditional rearranging of the table view.
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath: (NSIndexPath *) indexPath {
    return YES;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath {
    return YES;
}


- (void)didReceiveMemoryWarning
{
    UIAlertView *alertDialog;
    alertDialog = [[UIAlertView alloc]
                   initWithTitle:NSLocalizedString(@"Memory Problem", @"Hafıza Problemi")
                   message: NSLocalizedString(@"Please Close Some Applications!", @"Hafıza Problemi")
                   delegate: nil
                   cancelButtonTitle: @"Ok"
                   otherButtonTitles: nil];
    [alertDialog show];
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

@end