ios - UISearchBar - 不显示搜索结果

标签 ios objective-c uitableview search uisearchbar

在我的项目中,我使用自定义 UISearchBar。我完成了我的代码,但是当我尝试搜索任何内容时,它没有显示结果。 我正在分享一些代码片段。

.H文件

#import <UIKit/UIKit.h>

@interface FriendsViewController : UIViewController<UISearchDisplayDelegate , UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
    UITableView *friendslisttable;
    NSMutableArray *friendslist;
    NSMutableArray *friendsnamearray;
    NSMutableArray *profilepicarray;
    UIImageView * frndprofpic;
    UILabel *friendnamelabel;

    NSArray *searchResults;
}
@end

我的 .m 文件

#import "FriendsViewController.h"
#import "ProfileViewController.h"

@interface FriendsViewController ()

@end

@implementation FriendsViewController

- (id)init
{
    if (self = [super init])
    {
        self.title = @"Friends Request";
        self.view.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:125.0/255.0 blue:130.0/255.0 alpha:1];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Friends Request";

    UISearchBar *searchh = [[UISearchBar alloc] init];
    searchh.frame = CGRectMake(0 ,65, self.view.frame.size.width,45);
    searchh.delegate = self;
    searchh.showsBookmarkButton = NO;
    searchh.placeholder = @"Search friend";
    searchh.barTintColor = [UIColor whiteColor];
    [self.view addSubview:searchh];        
    friendslisttable = [[UITableView alloc]initWithFrame:CGRectMake(0,110 , self.view.frame.size.width, self.view.frame.size.height)] ;
    friendslisttable.delegate = self;
    friendslisttable. dataSource = self;
    [self.view addSubview:friendslisttable];

    friendsnamearray = [[NSMutableArray alloc]initWithObjects:@"friend1",@"friend12",@"friend13",@"friend14",@"friend15",@"friend16",@"friend17",@"friend18",@"friend19",@"friend20",@"friend21 ",@"friend22",@"", nil];

    profilepicarray = [[NSMutableArray alloc]initWithObjects:@"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", @"download13.jpeg", @"download16.jpeg",@"download3.jpeg", @"download6.jpg", @"download12.jpeg", @"download16.jpeg", @"  ",  nil];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [friendsnamearray filteredArrayUsingPredicate:resultPredicate];
}    

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

#pragma mark - TableView Delegate Method -------------------->>>>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return searchResults.count;
    }
    else {
        return friendsnamearray.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellidentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];

    if(!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    friendslisttable  = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        friendslisttable = [searchResults objectAtIndex:indexPath.row];
    } else
    {
        friendslisttable = [friendsnamearray objectAtIndex:indexPath.row];
    }

    frndprofpic = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];

    frndprofpic.image=[UIImage imageNamed:[profilepicarray objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:frndprofpic];

    friendnamelabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 250, 50)];
    friendnamelabel.text = [friendsnamearray objectAtIndex:indexPath.row];
    friendnamelabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];
    [cell.contentView addSubview:friendnamelabel];

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.row) {
        ProfileViewController *u = [[ProfileViewController alloc]init];
        [self.navigationController pushViewController:u animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}

@end

最佳答案

你需要什么:

  1. 像您在 viewDidLoad 中所做的那样创建搜索栏并为 searchBar 添加委托(delegate)作为自己(也完成了)

  2. 使用 <UISearchBarDelegate>

  3. 从委托(delegate)中实现一些方法

喜欢

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

在这个方法中,你应该过滤你的数据源并重新加载 tableView 中的数据。 .

如果一切正确 - 你会得到预期的行为

关于您的代码:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

将永远不会被调用 - 尝试将 breakPoint 放在里面 - 这是原因之一

另一个问题 - 是你的谓词 @"name contains %@" - 这意味着你在数组中有一个属性为 name 的对象.实际上你没有任何自定义对象 - 只有字符串,所以 @"SELF contains[c] %@"应该是

如果你更新它你会得到正确的搜索结果:

enter image description here

下一个问题 - tableViewCell 配置 if (tableView == self.searchDisplayController.searchResultsTableView) - 永远不会true - 如果你想使用它 - 你应该将基类更改为 UISearchDisplayController

因此,如果您更正此问题 - 您将获得可行的搜索解决方案。

此外,我建议您先阅读 Robert Martin 的“Clean Code” - ISBN-13: 978-0132350884


还有 this postthis one会有帮助

关于ios - UISearchBar - 不显示搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096922/

相关文章:

ios - 如何检测物理 body 区域的触摸?

swift - 如何从带有文本框的自定义单元格中获取 didselectrowat?

ios - SpriteKit 中的对象出现在屏幕底部

ios - 如何将大量数据加载到 CoreData 中

ios - Swift:无法将 NSNumber 类型转换为预期的参数类型 Int16

ios - 在 segues 之间切换 iOS 中的 UINavigationController 栏

ios - 从我的应用程序启动 Mail.app

objective-c - UITabBarController 中的 UINavigationController - 转到选项卡选择的顶部

ios - 将数据追加到 UITableView 的正确方法,swift

ios - 使用 dequeueReusableCell 重写单元格