mardi 4 août 2015

How to show search results with UISearchResultsUpdating in the new iOS8 Objective-C

I've been reading a lot about the new UISearchController and its protocols. and I saw many pages in the documentation also the sample code from Apple. But I couldn't implement it in my project -Xcode 6.4- iOS App.

Right now I need help on implementing the UISearchResultsUpdating protocol. I've this TableViewController "FYPBooksLibraryTableViewController.h" and I want to search its file names. when I run it every thing works, BUT the results are not shown at all.

From .m file:

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}

#pragma mark - UISearchControllerDelegate
- (void)presentSearchController:(UISearchController *)searchController {
}

- (void)willPresentSearchController:(UISearchController *)searchController {
}

- (void)didPresentSearchController:(UISearchController *)searchController {
}

- (void)willDismissSearchController:(UISearchController *)searchController {
}

- (void)didDismissSearchController:(UISearchController *)searchController {
}

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
//Waiting a fix
}

My data are in those 2 files: BooksData & FYPBooksObject

    //  BooksData.h

    #import <Foundation/Foundation.h>

    #define BOOK_NAME @"Book Name"
    #define BOOK_NUMBER_OF_PAGES @"Number of Pages"
    #define BOOK_LINK @"Book Path"

    @interface BooksData : NSObject
    + (NSArray *)allBooks;
    @end

    //  BooksData.m
    #import "BooksData.h"

    @implementation BooksData
    + (NSArray *)allBooks {
        NSMutableArray *booksInformation = [@[] mutableCopy];
        NSDictionary *book1Dictionary = @{BOOK_NAME : @"Book 1", BOOK_NUMBER_OF_PAGES : @"Number of pages: 43", BOOK_LINK : @"http://ift.tt/1ClMoQJ"};
        [booksInformation addObject:book1Dictionary];

    ...

    return [booksInformation copy];
    }

    @end

    //FYPBooksObject.h

    #import <Foundation/Foundation.h>

    @interface FYPBooksObject : NSObject

    @property (strong, nonatomic) NSString *nameB;
    @property (strong, nonatomic) NSString *numberOfPagesB;
    @property (strong, nonatomic) NSString *urlBook;

    -(id)initWithData:(NSDictionary *)data;

    @end

    //FYPBooksObject.m
        #import "FYPBooksObject.h"
        #import "BooksData.h"

        @implementation FYPBooksObject
        -(id)init {
            self = [self initWithData:nil];
            return self;
        }
        -(id)initWithData:(NSDictionary *)data {
            self = [super init];
            self.nameB = data[BOOK_NAME];
            self.numberOfPagesB = data[BOOK_NUMBER_OF_PAGES];
            self.urlBook = data[BOOK_LINK];
            return self;
        }

@end

Please bear with me and give me a full solution to search and display the results for books names in the same table view controller. And if there is a way to do an online search inside the books I'll appreciated..

Aucun commentaire:

Enregistrer un commentaire