iOS搜索栏不显示结果 [英] iOS search bar not showing results

查看:124
本文介绍了iOS搜索栏不显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*更新:这实际上是工作,我的自定义单元格的风格没有遇到,所以单元格看起来空白。那么如何让 searchResultsTableView 使用我的自定义单元格呢?

*Update: This actually works, the style for my custom cell hasn't come across and so the cell looks blank. How then do I get the searchResultsTableView to use my custom cell?

我在我的表格中实现了一个搜索栏视图。搜索,过滤所有工作,当我调试,但当我输入字符到搜索栏,结果不加载或显示。这是我所做的一切:

I have implemented a search bar in my table view. Searching, filtering all work when I debug, but when I enter characters into the search bar, the results do not load or show. This is everything I'm doing:

@interface InviteTableViewController ()<UIAlertViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>

@property(nonatomic, strong) NSNumber *contactsCount;
@property(nonatomic, strong) InviteTableViewCell *selectedCell;

@property(strong, nonatomic) NSMutableArray *filteredContactArray;
@property (weak, nonatomic) IBOutlet UISearchBar *contactsSearchBar;
@end

@implementation InviteTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) extractAllContacts: (ABAddressBookRef) addressBookRef{
    NSMutableArray *contactsArray = [NSMutableArray array];
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBookRef);

    for(int i = 0; i < numberOfPeople; i++){
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        if(firstName){
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

        for (CFIndex i = 0; i < ABMultiValueGetCount(emails); i++) {
            NSString *email = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(emails, i);
            MyUser *amUser = [[MyUser alloc] init];
            amUser.email =email;

            NSString *fullName =[NSString stringWithFormat:@"%@ %@",firstName, (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty))];
            amUser.fullName = fullName;
            [contactsArray addObject:amUser];
        }

        }

    }

    NSLog(@"================================count ============= %d", [contactsArray count]);
    contactsArray = [contactsArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *first = [(MyUser*) a fullName];
        NSDate *second = [(MyUser*)b fullName];
        return [first compare:second];
    }];
    self.inviteContactsArray = contactsArray;
    self.filteredContactArray = [NSMutableArray arrayWithCapacity:[contactsArray count]];

    [self.tableView reloadData];


}

- (void)viewDidLoad
{
    [super viewDidLoad];  

    _contactsCount = [NSNumber numberWithInt:0];


    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                [self extractAllContacts: addressBookRef];
                [self.tableView reloadData];
            } else {
                NSString *message = [NSString stringWithFormat:@"You have not given permission to use your address book.  Please allow in settings "];
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Enable Contacts" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

                [alert show];
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        [self extractAllContacts:addressBookRef];
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }

[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.filteredContactArray count];
    } else {
        return [self.inviteContactsArray count];
    }
}

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


    static NSString *CellIdentifier = @"InviteCell";

    InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( cell == nil ) {
        cell = [[InviteTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    MyUser *person = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {

        person = [self.filteredContactArray objectAtIndex:[indexPath row]];
        NSMutableArray *tempArray = self.filteredContactArray;

    }
    else
    {
        person = [self.inviteContactsArray objectAtIndex:[indexPath row]];
    }




    //InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    person = [self.inviteContactsArray objectAtIndex:indexPath.row];

    cell.nameLabel.text = person.fullName;
    cell.emailLabel.text = person.email;

    return cell;

}




#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {


    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [self.filteredContactArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.fullName contains[c] %@",searchText];
    self.filteredContactArray = [NSMutableArray arrayWithArray:[self.inviteContactsArray filteredArrayUsingPredicate:predicate]];


}

#pragma mark - UISearchDisplayController Delegate Methods

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

    return YES;
}


@end

它的外观:

推荐答案

同意@rdelmar。

Agree with @rdelmar.

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:在TableView中,如果你改变代码在

BUT There is a kind of tricky behavior in TableView, if you change the code in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    ....
}

InviteTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

如果添加前缀self。你的代码应该工作正常。

if added the prefix "self." your code should works fine.

if ( cell == nil )
 {
    cell = [[InviteTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
 }

它只会为你创建一个标准的字幕单元格,似乎不是你想要的,只是删除它们。

is unnecessary. it will just create a standard "Subtitle Cell" for you that seems not you want, just remove them.

这篇关于iOS搜索栏不显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆