我正在尝试创建一个搜索栏,它将用来自两个数据源(本地& API调用)的结果更新UITableView。我正在努力找出实现这一点的正确方法。filterContentForSearchText不在UITableView上显示数据
我有一个UITableView设置为顶部有搜索栏的用户列表。
当我有设置为与本地SEARCHTEXT过滤UITableCells代码它完美。
然而,从解析拉动数据,并试图加入,在细胞下方的本地搜索数据时没有出现。
的代码正确地更新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];
}
}
您是否实施了多少次浮动?在此处发布 – Pawan 2014-12-03 06:38:44
在完成从分析中加载数据后,您需要重新加载tableView。确保你在主队列上执行此操作 – Paulw11 2014-12-03 06:43:45
@pawan是的,我已经实现了numberOfRowsInSection。我已经相应地更新了我的帖子。 – 2014-12-03 17:15:20