如何使用UISearchResultsUpdating显示搜索结果

时间:2015-08-04 12:04:16

标签: ios objective-c xcode search

我一直在阅读很多关于新的UISearchController及其协议的内容。我在文档中看到许多页面也是Apple的示例代码。但我无法在我的项目-Xcode 6.4-iOS 8 App中实现它。

现在我需要有关实施UISearchResultsUpdating协议的帮助。 我已将UITableViewController设为FYPBooksLibraryTableViewController,我想搜索其文件名。当我运行它时一切正常,但结果根本没有显示。

来自.m文件:

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

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

我的数据在这2个文件中: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://www.example.com/file.pdf"};
        [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

请耐心等待,并给我一个完整的解决方案,以便在同一个表格视图控制器中搜索并显示图书名称的结果。 如果有办法在书中进行在线搜索,我会很感激..

1 个答案:

答案 0 :(得分:0)

我找到了答案,我不得不重建应用程序结构。 GitHub的示例代码帮助我做到了这一点。这是链接:https://github.com/dempseyatgithub/Sample-UISearchController