iphone - UISearch 不显示结果

标签 iphone ios objective-c uitableview parse-platform

我正在从解析中查询,当我尝试使用搜索栏进行搜索时,它返回“无结果”。我试图弄清楚我在显示对象时做错了什么,因为它确实在 NSLog 中显示它找到了对象。

我的 viewDidLoad:

   - (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;
    self.searchResults = [NSMutableArray array];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

TableView 的检索:

   -(void)filterResults:(NSString *)searchTerm {

    [self.searchResults removeAllObjects];

    PFQuery *query = [PFQuery queryWithClassName:@"New"];
    [query whereKeyExists:@"title"]; //this is based on whatever query you are trying to accomplish
    [query whereKey:@"title" containsString:searchTerm];

    NSArray *results  = [query findObjects];

    NSLog(@"%u", results.count);

    [self.searchResults addObjectsFromArray:results];
}

最后,不想工作的 tableview 单元格:

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

           static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject * testObject = [self.searchResults objectAtIndex:indexPath.row];
    cell.textLabel.text = [testObject objectForKey:@"title"];

    return cell;
    }

然后:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

然后从 Parse 加载到 UTTableview:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithClassName:@"New"];
    self = [super initWithCoder:aDecoder];
    if (self) {
        // The className to query on
        self.ClassName = @"New";

        //self.textKey = @"title";

        // The key of the PFObject to display in the label of the default cell style
        // self.keyToDisplay = @"text";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = NO;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = YES;

        self.loadingViewEnabled = NO;

        // The number of objects to show per page
        self.objectsPerPage = 50;
    }
    return self;

}

最佳答案

您可以使用另一种方式,借助搜索栏委托(delegate)方法。

例如 - 在头文件中包含 UISearchBarDelegate 以及 -

@property (strong, nonatomic) IBOutlet UISearchBar *searchSong; 

现在在你的 .m 文件中

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    temp = [self searchSongsData:self.searchSong.text from:`YourArray`];
} 

+ (NSMutableArray *) searchSongsData:(NSString *)searchtext from:(NSMutableArray *)array
{
    NSMutableArray *arrayToBeReturned = [[NSMutableArray alloc] init];
    NSArray *temp = [[NSArray alloc] initWithArray:array];

    NSString *textToBeSearch = [searchtext lowercaseString];

    SongsMoviesData *songs;

    for(int i = 0; i<temp.count; i++)
    {
        songs = [temp objectAtIndex:i];

        NSString *string1 = [songs.song_title lowercaseString];
        NSString *string2 = [songs.moview_name lowercaseString];
        NSString *string3 = [songs.singer_name lowercaseString];


    if ([string1 rangeOfString:textToBeSearch].location != NSNotFound ||[string2 rangeOfString:textToBeSearch].location != NSNotFound || [string3 rangeOfString:textToBeSearch].location != NSNotFound)
    {
       // NSLog(@"%@", songs.moview_name);
        [arrayToBeReturned addObject:songs];
    }
}

return arrayToBeReturned;

然后像这样将这个数组保存在您的搜索结果中。

关于iphone - UISearch 不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18457170/

相关文章:

iphone - ViewController 和通知中心( View ,不是设计模式 :p)

ios - 如何修复 AppDelegate for Firebase Notifications 中使用的已弃用代码?

ios - 强制下载 iOS 5.0.1 符号

iphone - 在 Controller 之间传递 UISegmentedControl 值...

iphone - 如何从 uitableview 中选择值并将其显示在上一页的按钮上

objective-c - 如何防止 slider 在边缘附近转动 "bad"

iphone - 停止滚动 ScrollView 中的最后一个对象

iphone - 在 UIPicker 中显示 CSV 文件的内容

iphone - 如何隐藏或删除 UINavigationController?

ios - 如何在应用程序名称中添加空格