我正在尝试将搜索栏添加到现有的表格视图中... 我已将searchBar属性添加到我的.m文件中,并添加了搜索栏委托,但我无济于事。我是新手,为我的代码添加搜索功能。
这是我的标题
#import
#import "SelectSingleView.h"
#import "SelectMultipleView.h"
#import "AddressBookView.h"
#import "FacebookFriendsView.h"
//-------------------------------------------------------------------------------------------------------------------------------------------------
@interface PeopleView : UITableViewController <SelectSingleDelegate, SelectMultipleDelegate, AddressBookDelegate, FacebookFriendsDelegate>
//-------------------------------------------------------------------------------------------------------------------------------------------------
@property (nonatomic, assign) IBOutlet id<PeopleView>delegate;
@end
这是.m文件
#import <Parse/Parse.h>
#import "ProgressHUD.h"
#import "utilities.h"
#import "PeopleView.h"
#import "ProfileView.h"
#import "SelectSingleView.h"
#import "SelectMultipleView.h"
#import "AddressBookView.h"
#import "FacebookFriendsView.h"
#import "NavigationController.h"
//-------------------------------------------------------------------------------------------------------------------------------------------------
@interface PeopleView()
{
BOOL skipLoading;
NSMutableArray *users;
NSMutableArray *userIds;
NSMutableArray *sections;
}
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@end
//-------------------------------------------------------------------------------------------------------------------------------------------------
@implementation PeopleView
//@synthesize delegate;
@synthesize searchBar;
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
{
[self.tabBarItem setImage:[UIImage imageNamed:@"tab_people"]];
self.tabBarItem.title = @"People";
//-----------------------------------------------------------------------------------------------------------------------------------------
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionCleanup) name:NOTIFICATION_USER_LOGGED_OUT object:nil];
}
return self;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)viewDidLoad
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[super viewDidLoad];
self.title = @"Friends";
self.navigationController.navigationBar.tintColor= [UIColor colorWithRed:(255/256.0) green:(128/256.0) blue:(0/256.0) alpha:(1.0)];
//---------------------------------------------------------------------------------------------------------------------------------------------
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(actionAdd)];
//---------------------------------------------------------------------------------------------------------------------------------------------
self.tableView.tableFooterView = [[UIView alloc] init];
//---------------------------------------------------------------------------------------------------------------------------------------------
users = [[NSMutableArray alloc] init];
userIds = [[NSMutableArray alloc] init];
// UIButton *facebookButton = [UIButton buttonWithType:UIButtonTypeSystem];
//[facebookButton setBackgroundImage:[[UIImage imageNamed:@"gibrlogo.png"];
//[facebookButton setTitle:@"Facebook Friends" forState:UIControlStateNormal];
// [facebookButton sizeToFit];
//facebookButton.center = CGPointMake(210.0, 30.0);// for center;
// Add an action in current code file (i.e. target)
//[facebookButton addTarget:self action:@selector(facebookButtonPressed:)
// forControlEvents:UIControlEventTouchUpInside];
//[self.view addSubview:facebookButton];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)viewDidAppear:(BOOL)animated
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[super viewDidAppear:animated];
//---------------------------------------------------------------------------------------------------------------------------------------------
if ([PFUser currentUser] != nil)
{
if (skipLoading) skipLoading = NO; else [self loadPeople];
}
else LoginUser(self);
}
#pragma mark - Backend methods
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)facebookButtonPressed:(UIButton *)facebookButton
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
skipLoading = YES;
FacebookFriendsView *facebookFriendsView = [[FacebookFriendsView alloc] init];
facebookFriendsView.delegate = self;
NavigationController *navController = [[NavigationController alloc] initWithRootViewController:facebookFriendsView];
[self presentViewController:navController animated:YES completion:nil];
NSLog(@"facebookButton Pressed");
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)loadPeople
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
PFQuery *query = [PFQuery queryWithClassName:PF_PEOPLE_CLASS_NAME];
[query whereKey:PF_PEOPLE_USER1 equalTo:[PFUser currentUser]];
[query includeKey:PF_PEOPLE_USER2];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
[users removeAllObjects];
[userIds removeAllObjects];
for (PFObject *people in objects)
{
PFUser *user = people[PF_PEOPLE_USER2];
[users addObject:user];
[userIds addObject:user.objectId];
}
[self setObjects:users];
[self.tableView reloadData];
}
else [ProgressHUD showError:@"Network error."];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchUsers:(NSString *)search_lower
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
PFQuery *query1 = [PFQuery queryWithClassName:PF_BLOCKED_CLASS_NAME];
[query1 whereKey:PF_BLOCKED_USER1 equalTo:[PFUser currentUser]];
PFQuery *query2 = [PFQuery queryWithClassName:PF_USER_CLASS_NAME];
[query2 whereKey:PF_USER_OBJECTID notEqualTo:[PFUser currentId]];
[query2 whereKey:PF_USER_OBJECTID doesNotMatchKey:PF_BLOCKED_USERID2 inQuery:query1];
[query2 whereKey:PF_USER_FULLNAME_LOWER containsString:search_lower];
[query2 orderByAscending:PF_USER_FULLNAME_LOWER];
[query2 setLimit:1000];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
[users removeAllObjects];
[users addObjectsFromArray:objects];
[self.tableView reloadData];
}
else [ProgressHUD showError:@"Network error."];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)setObjects:(NSArray *)objects
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (sections != nil) [sections removeAllObjects];
//---------------------------------------------------------------------------------------------------------------------------------------------
NSInteger sectionTitlesCount = [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
sections = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//---------------------------------------------------------------------------------------------------------------------------------------------
for (NSUInteger i=0; i<sectionTitlesCount; i++)
{
[sections addObject:[NSMutableArray array]];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
NSArray *sorted = [objects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
PFUser *user1 = (PFUser *)obj1;
PFUser *user2 = (PFUser *)obj2;
return [user1[PF_USER_FULLNAME] compare:user2[PF_USER_FULLNAME]];
}];
//---------------------------------------------------------------------------------------------------------------------------------------------
for (PFUser *object in sorted)
{
NSInteger section = [[UILocalizedIndexedCollation currentCollation] sectionForObject:object collationStringSelector:@selector(fullname)];
[sections[section] addObject:object];
}
}
#pragma mark - User actions
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionCleanup
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[users removeAllObjects];
[userIds removeAllObjects];
[sections removeAllObjects];
[self.tableView reloadData];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionAdd
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Search for a Friend" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) { [self actionSelectSingle]; }];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Add Friends" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) { [self actionSelectMultiple]; }];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"Invite Friends SMS" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) { [self actionAddressBook]; }];
UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"Invite Facebook Friends" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) { [self actionFacebookFriends]; }];
UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action1]; [alert addAction:action2]; [alert addAction:action3]; [alert addAction:action4]; [alert addAction:action5];
[self presentViewController:alert animated:YES completion:nil];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionSelectSingle
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
skipLoading = YES;
SelectSingleView *selectSingleView = [[SelectSingleView alloc] init];
selectSingleView.delegate = self;
NavigationController *navController = [[NavigationController alloc] initWithRootViewController:selectSingleView];
[self presentViewController:navController animated:YES completion:nil];
}
#pragma mark - SelectSingleDelegate
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)didSelectSingleUser:(PFUser *)user
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self addUser:user];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionSelectMultiple
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
skipLoading = YES;
SelectMultipleView *selectMultipleView = [[SelectMultipleView alloc] init];
selectMultipleView.delegate = self;
NavigationController *navController = [[NavigationController alloc] initWithRootViewController:selectMultipleView];
[self presentViewController:navController animated:YES completion:nil];
}
#pragma mark - SelectMultipleDelegate
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)didSelectMultipleUsers:(NSMutableArray *)users_
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
for (PFUser *user in users_)
{
[self addUser:user];
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionAddressBook
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
skipLoading = YES;
AddressBookView *addressBookView = [[AddressBookView alloc] init];
addressBookView.delegate = self;
NavigationController *navController = [[NavigationController alloc] initWithRootViewController:addressBookView];
[self presentViewController:navController animated:YES completion:nil];
}
#pragma mark - AddressBookDelegate
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)didSelectAddressBookUser:(PFUser *)user
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self addUser:user];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionFacebookFriends
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
skipLoading = YES;
FacebookFriendsView *facebookFriendsView = [[FacebookFriendsView alloc] init];
facebookFriendsView.delegate = self;
NavigationController *navController = [[NavigationController alloc] initWithRootViewController:facebookFriendsView];
[self presentViewController:navController animated:YES completion:nil];
}
#pragma mark - FacebookFriendsDelegate
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)didSelectFacebookUser:(PFUser *)user
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self addUser:user];
}
#pragma mark - Helper methods
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)addUser:(PFUser *)user
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if ([userIds containsObject:user.objectId] == NO)
{
PeopleSave([PFUser currentUser], user);
[users addObject:user];
[userIds addObject:user.objectId];
[self setObjects:users];
[self.tableView reloadData];
}
}
#pragma mark - Table view data source
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
return [sections count];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
return [sections[section] count];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if ([sections[section] count] != 0)
{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
else return nil;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
NSMutableArray *userstemp = sections[indexPath.section];
PFUser *user = userstemp[indexPath.row];
cell.textLabel.text = user[PF_USER_FULLNAME];
cell.detailTextLabel.text = user[PF_USER_EMAIL];
if(cell.detailTextLabel.text == nil){
cell.detailTextLabel.text = @"Facebook Account";
}
return cell;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
return YES;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
NSMutableArray *userstemp = sections[indexPath.section];
PFUser *user = userstemp[indexPath.row];
//---------------------------------------------------------------------------------------------------------------------------------------------
PeopleDelete([PFUser currentUser], user);
//---------------------------------------------------------------------------------------------------------------------------------------------
[users removeObject:user];
[userIds removeObject:user.objectId];
[self setObjects:users];
//---------------------------------------------------------------------------------------------------------------------------------------------
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark - Table view delegate
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//---------------------------------------------------------------------------------------------------------------------------------------------
NSMutableArray *userstemp = sections[indexPath.section];
PFUser *user = userstemp[indexPath.row];
//---------------------------------------------------------------------------------------------------------------------------------------------
ProfileView *profileView = [[ProfileView alloc] initWith:nil User:user];
profileView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:profileView animated:YES];
}
#pragma mark - UISearchBarDelegate
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if ([searchText length] > 0)
{
[self searchUsers:[searchText lowercaseString]];
}
else [self loadPeople];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar_
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[searchBar_ setShowsCancelButton:YES animated:YES];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar_
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[searchBar_ setShowsCancelButton:NO animated:YES];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar_
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self searchBarCancelled];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar_
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[searchBar_ resignFirstResponder];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)searchBarCancelled
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
searchBar.text = @"";
[searchBar resignFirstResponder];
[self loadPeople];
}
@end