我正在尝试创建一个搜索栏,它将使用两个数据源(本地和API调用)的结果更新UITableView。我正在努力找出实现这个目标的正确方法。
我将UITableView设置为顶部有搜索栏的用户列表。
当我将代码设置为使用searchText在本地过滤UITableCells时,它可以很好地工作。
然而,当从Parse中提取数据并尝试在本地搜索数据下方的单元格中添加数据时,不会出现任何内容。
代码正确更新self.searchText
数组,但数据永远不会显示在tableView中。是什么给了什么?
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.searchResult removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
NSMutableArray *queryArray = [[NSMutableArray alloc] init];
PFQuery *query = [PFUser query];
[query whereKey:@"fullName" hasPrefix:@"Joe"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *pfObj in objects)
{
NSDictionary *person = [[NSDictionary alloc]
initWithObjects:@[pfObj[@"fullName"], pfObj.objectId]
forKeys:@[@"name", @"objectId"]];
[queryArray addObject:person];
}
self.searchResult = [NSMutableArray arrayWithArray: [self.contacts filteredArrayUsingPredicate:resultPredicate]];
[self.searchResult addObjectsFromArray:queryArray];
} else {
NSLog(@"%@", error);
}
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResult[indexPath.row] objectForKey:@"name"];
}
else
{
cell.textLabel.text = [self.contacts[indexPath.row] objectForKey:@"name"];
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResult count];
}
else
{
return [self.contacts count];
}
}
答案 0 :(得分:1)
也许您从服务器获取数据,以便在调用方法
时- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
它使用当前没有值的当前数组self.searchResult
重新加载表。因此,我认为您应该在从
if (!error) {
for (PFObject *pfObj in objects)
{
NSDictionary *person = [[NSDictionary alloc]
initWithObjects:@[pfObj[@"fullName"], pfObj.objectId]
forKeys:@[@"name", @"objectId"]];
[queryArray addObject:person];
}
self.searchResult = [NSMutableArray arrayWithArray: [self.contacts filteredArrayUsingPredicate:resultPredicate]];
[self.searchResult addObjectsFromArray:queryArray];
[self.searchDisplayController.searchResultsTableView reloadData];
} else {
NSLog(@"%@", error);
}
根据我的说法,最好的方法是首先显示本地结果 - 直到其余的联系人从Parse加载,即。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.searchResult removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
self.searchResult = [NSMutableArray arrayWithArray: [self.contacts filteredArrayUsingPredicate:resultPredicate]];
NSMutableArray *queryArray = [[NSMutableArray alloc] init];
PFQuery *query = [PFUser query];
[query whereKey:@"fullName" hasPrefix:@"Joe"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *pfObj in objects)
{
NSDictionary *person = [[NSDictionary alloc]
initWithObjects:@[pfObj[@"fullName"], pfObj.objectId]
forKeys:@[@"name", @"objectId"]];
[queryArray addObject:person];
}
[self.searchResult addObjectsFromArray:queryArray];
self.searchDisplayController.searchResultsTableView reloadData];
} else {
NSLog(@"%@", error);
}
}];
}