gpt4 book ai didi

ios - 在文本更改时重新加载表格时表格中没有任何显示

转载 作者:行者123 更新时间:2023-11-29 02:06:14 24 4
gpt4 key购买 nike

我有一个搜索栏,用户可以在其中过滤用户名。用户输入的字符串发布到服务器并以过滤后的用户数组作为响应。我正在尝试将这些用户加载到 TableView 中,当用户更改搜索项时,它会经历相同的过程 -

#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

最佳答案

检查

    NSLog(@"%@", searchResults);

你要去那里做什么?另外,每次搜索后刷新数据时,都应该清除结果数组

[searchResults removeAllObjects]

关于ios - 在文本更改时重新加载表格时表格中没有任何显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29762799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com