好的,我有一个PFQueryTableViewController从Parse中提取数据,然后你可以搜索它并选择一行。如果您只选择一行它可以正常工作但如果您搜索然后单击它它会返回"第一个"所有数据中的行不是单击的行。一切正常,我从来没有改变过代码,但我把tableviewcontroller放在一个容器中,这就是为什么它发生了。看到问题我将其从容器中删除但问题仍然存在!我不知道该怎么做!这是我的代码
#import "AllDataTableViewController.h"
#import <Parse/Parse.h>
@interface AllDataTableViewController () <UISearchDisplayDelegate, UISearchBarDelegate> {
}
@end
@implementation AllDataTableViewController : PFQueryTableViewController
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;
-(void)viewDidAppear:(BOOL)animated{
self.searchDisplayController.searchResultsTableView.delegate = self;
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"popAgain"] isEqualToString:@"true"])
{
[self.navigationController popViewControllerAnimated:YES];
NSString *popAgain = @"false";
[[NSUserDefaults standardUserDefaults] setObject:popAgain forKey:@"popAgain"];
}
}
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// The className to query on
self.parseClassName = @"TruthIsData";
// The key of the PFObject to display in the label of the default cell style
self.textKey = @"truthIsName";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
NSLog(self.objects);
NSLog(@"objects");
} else {
return self.searchResults.count;
}
}
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"TruthIsData"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSArray *results = [NSArray arrayWithArray:objects];
NSLog(@"%@", results);
NSLog(@"%lu", (unsigned long)results.count);
NSLog(@"results^");
[self.searchResults addObjectsFromArray:results];
NSPredicate *searchPredicate =
[NSPredicate predicateWithFormat:@"SELF.truthIsName contains[c] %@",searchTerm];
searchResults = [NSMutableArray arrayWithArray:[results filteredArrayUsingPredicate:searchPredicate]];
[self.searchDisplayController.searchResultsTableView reloadData];
NSLog(@"%@", searchResults);
NSLog(@"%lu", (unsigned long)searchResults.count);
NSLog(@"search results^");
}];
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
return query;
[query orderByAscending:@"createdAt"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
if (tableView == self.tableView) {
cell.textLabel.text = [object objectForKey:@"truthIsName"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.detailTextLabel.text = [NSString stringWithFormat:@"User: %@", [object objectForKey:@"username"]];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
PFObject* object = self.searchResults[indexPath.row];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [object objectForKey:@"truthIsName"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.detailTextLabel.text = [NSString stringWithFormat:@"User: %@", [object objectForKey:@"username"]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PFObject *obj = [self.objects objectAtIndex:indexPath.row];
NSString *quickId = obj.objectId;
[[NSUserDefaults standardUserDefaults] setObject:quickId forKey:@"selectedTI"];
//NSLog(quickId);
[self performSegueWithIdentifier:@"selectedRequest" sender:self];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
@end
我已经尝试将此添加到视图确实显示但它没有帮助
searchDisplayController.searchResultsDelegate = self;
答案 0 :(得分:0)
在didSelectRowAtIndexPath中,您还需要检查tableview是否是搜索表:
if (tableView == self.searchDisplayController.searchResultsTableView) {
PFObject* object = self.searchResults[indexPath.row];
}
然后做你需要做的对象......