我有一个搜索栏,用户可以在其中过滤用户名。用户输入的字符串会发布到服务器并通过过滤的用户数组进行响应。我正在尝试将这些用户加载到表格视图中,当用户更改搜索项目时,它会经历相同的过程 -
#import "AddFriend.h"
@implementation AddFriend
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)viewDidLoad{
_searchBar.delegate = self;
searchResults = [[NSMutableArray alloc]init];
myTableView.dataSource = self;
myTableView.delegate = self;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[searchResults removeAllObjects];
_searchString = searchBar.text;
[self postSearch];
}
-(void)postSearch{
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/api/v1/users/search"]];
// Specify that it will be a POST request
request.HTTPMethod = @"POST";
// This is how we set header fields
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// Convert your data and set your request's HTTPBody property
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:
_searchString, @"username",
nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];
[request setHTTPBody:postdata];
// Create url connection and fire request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
#pragma mark - NSURL Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSError *error = nil;
NSArray *json = [NSJSONSerialization
JSONObjectWithData:_responseData
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for (int i = 0; i < json.count; i++) {
NSDictionary *resultsDictionary = [json objectAtIndex:i];
NSString *username = [resultsDictionary objectForKey:@"username"];
[searchResults addObject:username];
}
NSLog(@"%@", searchResults);
[myTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [searchResults count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (searchResults.count > 1) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
}
return cell;
}
@end
答案 0 :(得分:1)
检查
你到底在那里干什么? 此外,每次在搜索后刷新数据时,都应清除结果数组NSLog(@"%@", searchResults);
[searchResults removeAllObjects]