我目前使用TreeHouse教程制作了类似SnapChat的应用程序。
构建应用程序的方式,当您转到朋友,然后单击右上角的编辑时,它会向您显示已注册的所有人。我不想要这个,而是有人必须搜索用户名。
我已在编辑好友视图控制器中添加了一个搜索栏,并将其连接到.m文件。
我正在使用此帖子的帮助,但搜索栏在按下时不会显示任何用户:https://teamtreehouse.com/forum/search-friends-ribbit-almost-complete
这是我目前编辑EditFriendsViewController.h的代码
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface EditFriendsViewController : UITableViewController <UISearchBarDelegate>
@property (nonatomic, strong) NSArray *allUsers;
@property (nonatomic, strong) PFUser *currentUser;
@property (nonatomic, strong) NSMutableArray *friends;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) PFUser *user;
- (BOOL)isFriend:(PFUser *)user;
@end
这是我目前的EditFriendsViewController.m
代码#import "EditFriendsViewController.h"
#import "MSCellAccessory.h"
@interface EditFriendsViewController ()
@end
@implementation EditFriendsViewController
UIColor *disclosureColor;
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar.delegate = self;
PFQuery *query = [PFUser query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
//set your user property to the first (and only) object in the objects array
self.user = [objects objectAtIndex:0];
[self.tableView reloadData];
}
}];
self.currentUser = [PFUser currentUser];
disclosureColor = [UIColor colorWithRed:0.553 green:0.439 blue:0.718 alpha:1.0];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//dismiss keyboard
[self.searchBar resignFirstResponder];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//Enable the cancel button when the user touches the search field
self.searchBar.showsCancelButton = TRUE;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
//dsiable the cancel button when the user ends editing
self.searchBar.showsCancelButton = FALSE;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
//dismiss keyboard
[self.searchBar resignFirstResponder];
//Strip the whitespace off the end of the search text
NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
//Check to make sure the field isnt empty and Query parse for username in the text field
if (![searchText isEqualToString:@""]) {
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:searchText];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//check to make sure the query actually found a user
if (objects.count > 0) {
//found a user
//set the user as the table views data source and reload the table view
//A user was not found, display error message
} else {
//no user found
}
[self.tableView reloadData];
} else {
//error occurred
}
}];
}
}
#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.
return [self.allUsers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = self.user.username;
if ([self isFriend:user]) {
cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_CHECKMARK color:disclosureColor];
}
else {
cell.accessoryView = nil;
}
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];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];
if ([self isFriend:user]) {
cell.accessoryView = nil;
for (PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
[self.friends removeObject:friend];
break;
}
}
[friendsRelation removeObject:user];
}
else {
cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_CHECKMARK
color:disclosureColor];
[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
- (BOOL)isFriend:(PFUser *)user {
for (PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
return YES;
}
}
return NO;
}
@end
答案 0 :(得分:0)
如果您有//found a user
,请替换为:
self.allUsers = objects;
这应该会显示结果。