• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Was gibt es Schöneres als den Mai draußen in der Natur mit allen Sinnen zu genießen? Lasst uns teilhaben an Euren Erlebnissen und macht mit beim Thema des Monats Da blüht uns was! ---> Klick

[Objective-C] UISearchDisplayController

Markussm

Granny Smith
Registriert
20.12.14
Beiträge
14
Ich wünsche euch ein schönes neues Jahr 2016

Hallo ihr, hab mal eine Frage an die ob mir jemand helfen kann.
Ich hänge bei einem Problem fest, ich will mein komplettes Projekt nur ab iOS Version 8.1 bereitstellen.
Nun hab ich das Problem ich komme beim UISearchDisplayController ( dieser wird ja seit 8.0 nicht weiter "unterstützt" ) nicht weiter.
Hier mal die Header Datei
Code:
#import <UIKit/UIKit.h>
#import "StatBestimmtView.h"
#import "GleichgewView.h"
#import "KraftDrehmomentView.h"
@interface StatMenView : GAITrackedViewController <UISearchBarDelegate, UISearchControllerDelegate>
{
    UITableView *ivMainTableView;
    NSMutableArray *arryweiterFunktionen;
}
@property(strong,nonatomic) NSMutableArray *filteredCandyArray;
@property IBOutlet UISearchBar *candySearchBar;
@property (nonatomic, strong)StatBestimmtView* statview;
@property (nonatomic, strong) IBOutlet UITableView *mainTableView;
@property(nonatomic, strong)KraftDrehmomentView* momentview;
@property(nonatomic, strong)GleichgewView* gleichview;

@end

Hier die .m Datei
Code:
#import "StatMenView.h"
@implementation StatMenView
@synthesize momentview,statview,gleichview, filteredCandyArray, candySearchBar;
@synthesize mainTableView = ivMainTableView;
   
- (void)viewDidLoad {
    [candySearchBar setShowsScopeBar:NO];
    [candySearchBar sizeToFit];
    CGRect newBounds = [[self mainTableView] bounds];
    newBounds.origin.y = newBounds.origin.y + candySearchBar.bounds.size.height;
    [[self mainTableView] setBounds:newBounds];
   
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title =  NSLocalizedString(@"statik", nil);
    arryweiterFunktionen = [[[NSArray alloc] initWithObjects: NSLocalizedString(@"statfachti", nil), NSLocalizedString(@"Moment", nil), NSLocalizedString(@"Gleichgewicht", nil),nil] mutableCopy] ;
   
    filteredCandyArray = [NSMutableArray arrayWithCapacity:[arryweiterFunktionen count]];
    // Reload the table
    [[self mainTableView] reloadData];
   
    [super viewDidLoad];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if (tableView == self.searchDisplayController.searchResultsTableView){
        return [filteredCandyArray count];
    }else {
        return [arryweiterFunktionen count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [filteredCandyArray objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [arryweiterFunktionen objectAtIndex:[indexPath row]];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        if([[filteredCandyArray objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"statfachti", nil)]) {
            StatBestimmtView *viewTwo = [[StatBestimmtView alloc] initWithNibName:@"StatBestimmtView" bundle:[NSBundle mainBundle]];
            self.statview = viewTwo;
            [self.navigationController pushViewController:self.statview animated:YES];
        }
    }
    else{
        if([[arryweiterFunktionen objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"Gleichgewicht", nil)]) {
            GleichgewView *viewTwo = [[GleichgewView alloc] initWithNibName:@"GleichgewView" bundle:[NSBundle mainBundle]];
            self.gleichview = viewTwo;
            [self.navigationController pushViewController:self.gleichview animated:YES];
        }
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ( [[segue identifier] isEqualToString:@"candyDetail"] ) {
        UIViewController *candyDetailViewController = [segue destinationViewController];
        if(sender == self.searchDisplayController.searchResultsTableView) {
            NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }
        else {
            NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
            NSString *destinationTitle = [[arryweiterFunktionen objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }
       
    }
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
    [filteredCandyArray removeAllObjects];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    filteredCandyArray = [NSMutableArray arrayWithArray: [arryweiterFunktionen filteredArrayUsingPredicate:resultPredicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}
@end


Ich hoffe mir kann jemand helfen den Code mit z.B. if (tableView == self.searchDisplayController.searchResultsTableView) auf UISearchController "umzustellen".
Ich habe es schon mit if([UISearchController class]) probiert, hat aber nicht funktioniert.

Gruß Markus
 

Markussm

Granny Smith
Registriert
20.12.14
Beiträge
14
Ich habe es jetzt mal mit den Anleitungen probiert. Ich komme aber jetzt nicht mehr weiter hier ist mal der code
Hier mal die Header Datei
Code:
#import <UIKit/UIKit.h>
#import "StatBestimmtView.h"
#import "GleichgewView.h"
#import "KraftDrehmomentView.h"
@interface StatMenView : GAITrackedViewController <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UISearchDisplayDelegate>
{
 
    UITableView *ivMainTableView;
    NSMutableArray *arryweiterFunktionen;
}
@property(strong,nonatomic) NSMutableArray *filteredCandyArray;
@property IBOutlet UISearchBar *candySearchBar;
@property (nonatomic, strong)StatBestimmtView* statview;
@property (nonatomic, strong) IBOutlet UITableView *mainTableView;
@property(nonatomic, strong)KraftDrehmomentView* momentview;
@property(nonatomic, strong)GleichgewView* gleichview;


@property (strong, nonatomic) UISearchController *searchController;

@end

Hier die .m Datei

Code:
#import "StatMenView.h"
@implementation StatMenView
@synthesize momentview,statview,gleichview;
@synthesize mainTableView = ivMainTableView;
@synthesize filteredCandyArray;
@synthesize candySearchBar;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.screenName = @"Statik-Menu";
}
 
- (void)viewDidLoad {
    [candySearchBar setShowsScopeBar:NO];
    [candySearchBar sizeToFit];
    CGRect newBounds = [[self mainTableView] bounds];
    newBounds.origin.y = newBounds.origin.y + candySearchBar.bounds.size.height;
    [[self mainTableView] setBounds:newBounds];
 
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title =  NSLocalizedString(@"statik", nil);
    arryweiterFunktionen = [[[NSArray alloc] initWithObjects: NSLocalizedString(@"statfachti", nil), NSLocalizedString(@"Moment", nil), NSLocalizedString(@"Gleichgewicht", nil),nil] mutableCopy] ;
 
    filteredCandyArray = [NSMutableArray arrayWithCapacity:[arryweiterFunktionen count]];
    // Reload the table
    [[self mainTableView] reloadData];
 
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self action:@selector(longPressGestureRecognized:)];
    [self.mainTableView addGestureRecognizer:longPress];
  
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
 
    [super viewDidLoad];

}

- (IBAction)longPressGestureRecognized:(id)sender {
    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;
    CGPoint location = [longPress locationInView:self.mainTableView];
    NSIndexPath *indexPath = [self.mainTableView indexPathForRowAtPoint:location];
    static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.
    static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.
    switch (state) {
        case UIGestureRecognizerStateBegan: {
            if (indexPath) {
                sourceIndexPath = indexPath;
                UITableViewCell *cell = [self.mainTableView cellForRowAtIndexPath:indexPath];
                // Take a snapshot of the selected row using helper method.
                snapshot = [self customSnapshoFromView:cell];
                // Add the snapshot as subview, centered at cell's center...
                __block CGPoint center = cell.center;
                snapshot.center = center;
                snapshot.alpha = 0.0;
                [self.mainTableView addSubview:snapshot];
                [UIView animateWithDuration:0.25 animations:^{
                    // Offset for gesture location.
                    center.y = location.y;
                    snapshot.center = center;
                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapshot.alpha = 0.98;
                    cell.alpha = 0.0;
                } completion:^(BOOL finished) {
                    cell.hidden = YES;
                }];
            }
            break;
        }
        case UIGestureRecognizerStateChanged: {
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;
            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
                // ... update data source.
                [arryweiterFunktionen exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
                // ... move the rows.
                [self.mainTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;
            }
            break;
        }
        default: {
            // Clean up.
            UITableViewCell *cell = [self.mainTableView cellForRowAtIndexPath:sourceIndexPath];
            cell.hidden = NO;
            cell.alpha = 0.0;
            [UIView animateWithDuration:0.25 animations:^{
                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                cell.alpha = 1.0;
            } completion:^(BOOL finished) {
                sourceIndexPath = nil;
                [snapshot removeFromSuperview];
                snapshot = nil;
             
            }];
            break;
        }
    }
}
/** @brief Returns a customized snapshot of a given view. */
- (UIView *)customSnapshoFromView:(UIView *)inputView {
    // Make an image from the input view.
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Create an image view.
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;
    return snapshot;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchController.active){
   //if (tableView == self.searchDisplayController.searchResultsTableView){
        return [filteredCandyArray count];
    }else {
        return [arryweiterFunktionen count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (self.searchController.active){
        //if (tableView == self.searchDisplayController.searchResultsTableView){
        cell.textLabel.text = [filteredCandyArray objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [arryweiterFunktionen objectAtIndex:[indexPath row]];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Suche -> next Nib
    if (self.searchController.active){
        //if (tableView == self.searchDisplayController.searchResultsTableView){
     
        if([[filteredCandyArray objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"statfachti", nil)]) {
            StatBestimmtView *viewTwo = [[StatBestimmtView alloc] initWithNibName:@"StatBestimmtView" bundle:[NSBundle mainBundle]];
            self.statview = viewTwo;
            [self.navigationController pushViewController:self.statview animated:YES];
        }
        if([[filteredCandyArray objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"Gleichgewicht", nil)]) {
            GleichgewView *viewTwo = [[GleichgewView alloc] initWithNibName:@"GleichgewView" bundle:[NSBundle mainBundle]];
            self.gleichview = viewTwo;
            [self.navigationController pushViewController:self.gleichview animated:YES];
        }
     
        if([[filteredCandyArray objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"Moment", nil)]) {
            KraftDrehmomentView *viewTwo = [[KraftDrehmomentView alloc] initWithNibName:@"KraftDrehmomentView" bundle:[NSBundle mainBundle]];
            self.momentview = viewTwo;
            [self.navigationController pushViewController:self.momentview animated:YES];
        }
    }
    else{
        if([[arryweiterFunktionen objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"Gleichgewicht", nil)]) {
            GleichgewView *viewTwo = [[GleichgewView alloc] initWithNibName:@"GleichgewView" bundle:[NSBundle mainBundle]];
            self.gleichview = viewTwo;
            [self.navigationController pushViewController:self.gleichview animated:YES];
        }
     
        if([[arryweiterFunktionen objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"Moment", nil)]) {
            KraftDrehmomentView *viewTwo = [[KraftDrehmomentView alloc] initWithNibName:@"KraftDrehmomentView" bundle:[NSBundle mainBundle]];
            self.momentview = viewTwo;
            [self.navigationController pushViewController:self.momentview animated:YES];
        }
     
        if([[arryweiterFunktionen objectAtIndex:indexPath.row ]isEqual:NSLocalizedString(@"statfachti", nil)]) {
            StatBestimmtView *viewTwo = [[StatBestimmtView alloc] initWithNibName:@"StatBestimmtView" bundle:[NSBundle mainBundle]];
            self.statview = viewTwo;
            [self.navigationController pushViewController:self.statview animated:YES];
        }
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ( [[segue identifier] isEqualToString:@"candyDetail"] ) {
        UIViewController *candyDetailViewController = [segue destinationViewController];
        if (self.searchController.active){
            //if (tableView == self.searchDisplayController.searchResultsTableView){
            NSIndexPath *indexPath = [self.searchController.searchResultsController indexPathForSelectedRow];
         
            //NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }
        else {
            NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
            NSString *destinationTitle = [[arryweiterFunktionen objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }
     
    }
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
    [filteredCandyArray removeAllObjects];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    filteredCandyArray = [NSMutableArray arrayWithArray: [arryweiterFunktionen filteredArrayUsingPredicate:resultPredicate]];
    [self.mainTableView reloadData];
}

/*-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}*/


#pragma mark -
#pragma mark === UISearchBarDelegate ===
#pragma mark -

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self updateSearchResultsForSearchController:self.searchController];
}

#pragma mark -
#pragma mark === UISearchResultsUpdating ===
#pragma mark -

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    NSString *inStr = [@(searchController.searchBar.selectedScopeButtonIndex) stringValue];

    //[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
 
    [self filterContentForSearchText:searchString scope:inStr];
    [self.mainTableView reloadData];
}

-(void) setupSearchController {
    _searchController.searchResultsUpdater = self;
 
}

-(void)viewDidUnload{
    [super viewDidUnload];
}
@end

Mein Problem ist das keine Ergebnisse angezeigt werden. Es wird nur die normale Liste mit den drei Einträgen angezeigt wenn ich nach was suche.

Ich habe die vermutung, das das Problem bei - (void)prepareForSegue: (UIStoryboardSegue *)segue sender: (id)sender liegt.
Kann mir jemand Helfen ?


Gruß Markus