简体   繁体   English

“取消按钮”之后搜索栏开始工作

[英]Search bar is working AFTER “Cancel Button”

I've made a view for editing friend, with a searchbar. 我已经通过搜索栏为编辑朋友创建了一个视图。 My adding/deleting friends is working fine, but I've a problem with my adding/deleting friends WITH SEARCHBAR... My searchbar finds well the email I'm tapping, but the order change : if I find 2 email with my searchbar, the 2 email will are the 2 first email in my property "Allfriend", and not the email I found with searchbar... Is there any code to complete in didSelectRowAtIndexPath after NSLog ? 我的添加/删除朋友工作正常,但是我在使用SEARCHBAR的添加/删除朋友时遇到了问题...我的搜索栏找到了我正在点击的电子邮件,但是顺序发生了变化:如果我在搜索栏中找到了2个电子邮件,这2封电子邮件将是我的属性“ Allfriend”中的2封第一封电子邮件,而不是我在搜索栏中找到的电子邮件... NSLog之后,是否在didSelectRowAtIndexPath中完成任何代码?

I let you see my code, tell me if you see a problem : 我让您看一下我的代码,告诉我是否遇到问题:

editfriend.h : editfriend.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface EditFriendsViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>


@property (nonatomic, strong) NSArray *allUsers;
@property (nonatomic, strong) PFUser *currentUser;
@property (nonatomic, strong) NSMutableArray *friends;
@property (strong, nonatomic) NSArray *searchResults;
@property (nonatomic, strong) PFUser *user;
@property (nonatomic, strong) NSMutableArray *filteredArray;


@property (nonatomic, strong) UIImage *MindleNav;



-(BOOL)isFriend:(PFUser*)user;

@end

editfriend.m : editfriend.m:

#import "EditFriendsViewController.h"

@interface EditFriendsViewController ()

@end

@implementation EditFriendsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:self.MindleNav];

    self.searchResults = [[NSArray alloc] init];


    PFQuery *query = [PFUser query];
    [query orderByAscending:@"email"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else {
            self.allUsers = objects;
            //            self.user = [objects objectAtIndex:0];
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    }];

    self.currentUser = [PFUser currentUser];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResults count];
    }
    else
    {
        return [self.allUsers count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [[self.searchResults objectAtIndex:indexPath.row] email];
    } else {
        cell.textLabel.text = user.email;
    }


    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}


#pragma mark - Table view delegate
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];

//Edit Friends with searchbar
if (tableView == self.searchDisplayController.searchResultsTableView) {
    PFUser *user = [self.searchResults objectAtIndex:indexPath.row];

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

        for(PFUser *friend in self.searchResults) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                [self.friends removeObject:friend];
                break;
            }
        }

        [friendsRelation removeObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.friends addObject:user];
        [friendsRelation addObject:user];
        [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

    //Edit Friends
} else {
    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryNone;

        for(PFUser *friend in self.friends) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                [self.friends removeObject:friend];
                break;
            }
        }

        [friendsRelation removeObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.friends addObject:user];
        [friendsRelation addObject:user];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

} }

#pragma mark - Helper methods
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email beginswith[c] %@", searchText];
    self.searchResults = [self.allUsers filteredArrayUsingPredicate:predicate];

}

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

    return YES;
}


- (BOOL)isFriend:(PFUser *)user {
    for(PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            return YES;
        }
    }

    return NO;
}

@end

You're calling [self.tableView reloadData] on a background thread (due to findObjectsInBackground ), so your UI won't update right away. 您正在后台线程上调用[self.tableView reloadData] (由于findObjectsInBackground ),因此您的UI不会立即更新。

If you want the changes to take place instantly, you need to rewrite that line to: 如果要立即进行更改,则需要将该行重写为:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

Also, since you already do a query for all objects in viewDidLoad , when the user would like to search, you shouldn't query again, but instead have a property called filteredUsers , which holds the user you'd like to display. 另外,由于您已经对viewDidLoad所有对象进行了查询,因此当用户想要搜索时,您无需再次查询,而是拥有一个名为filteredUsers的属性,该属性保存了您要显示的用户。

You can filter an array with: 您可以使用以下方法过滤数组:

self.filteredArray = [self.allUsers filteredArrayUsingPredicate:predicate];

You can take a look here on how to create an NSPredicate . 您可以在此处了解如何创建NSPredicate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM