1

私のアプリでは、データベース上のデータの所有者として Parse.com を使用し、研究のために SearchBarDisplayController を使用しています。別のView Controllerでデータセルを送信するためにテーブルを準備するために次の準備をしましたが、完全に機能します...今、問題が発生しています...

SearchDisplayController の「prepareforsegue」を作成して、検索結果のセルをアクティブにするにはどうすればよいですか?

ここでは、私が作成した「セグエの準備」(または別のメソッド)のコードと、渡されるデータを確認できます...

これに関するいくつかのアイデアはありますか?本当にありがとう!

#import "Ricerca.h"
#import "Custom.h"
#import "DettagliProfilo.h"

@interface Ricerca () <UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic, strong) NSMutableArray *searchResults;

@end

@implementation Ricerca
@synthesize searchResults;




- (void)viewDidLoad {


 [super viewDidLoad];
    [self loadObjects];
    self.searchResults = [NSMutableArray array];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // The className to query on
        self.parseClassName = @"_User";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = YES;
       self.objectsPerPage = 10;
    }
    return self;
}

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    //[query whereKeyExists:@"username"];
    //[query whereKeyExists:@"email"];
    [query orderByAscending:@"username"];

    if ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    return query;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {



    UITableViewCell *cell = (UITableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }


    // Configure the cell

    UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    cell.detailTextLabel.backgroundColor = color;
    cell.textLabel.backgroundColor = color;


    if (tableView == self.tableView) {
        cell.textLabel.text = [object objectForKey:@"username"];
        cell.detailTextLabel.text = [object objectForKey:@"email"];
        cell.imageView.image = [UIImage imageNamed:@"unknown"];
        PFFile *imageFile = [object objectForKey:@"foto"];
        [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            cell.imageView.image =[UIImage imageWithData:data];


        }];

    }

    else if(tableView == self.searchDisplayController.searchResultsTableView) {


        PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
        NSString *content = [searchedUser objectForKey:@"username"];
        NSString *email = [searchedUser objectForKey:@"email"];
        cell.detailTextLabel.text = email;
        cell.textLabel.text = content;
        cell.imageView.image = [UIImage imageNamed:@"unknown"];
        PFFile *imageFile = [searchedUser objectForKey:@"foto"];
        [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            cell.imageView.image =[UIImage imageWithData:data];


        }];


        NSLog(@"Content: %@", content);


    }

    return cell;

}
- (void)callbackLoadObjectsFromParse:(NSArray *)result error:(NSError *)error {
    if (!error) {

        [self.searchResults removeAllObjects];

        NSLog(@"Successfully fetched %d entries", result.count);

        [self.searchResults addObjectsFromArray:result];
        [self.searchDisplayController.searchResultsTableView reloadData];

    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

}


-(void)filterResults:(NSString *)searchTerm {

    [self.searchResults removeAllObjects];

    PFQuery *query = [PFUser query];
    [query orderByAscending:@"username"];
    [query whereKeyExists:@"email"];
    [query whereKey:@"username" containsString:searchTerm];
     query.cachePolicy = kPFCachePolicyNetworkOnly;
    [query findObjectsInBackgroundWithTarget:self selector:@selector(callbackLoadObjectsFromParse:error:)];
  }




-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    [self filterResults:searchString];

    return YES;
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 80.0f; // or some other height
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {
        return self.objects.count;

    } else {
        return self.searchResults.count;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [super tableView:tableView didSelectRowAtIndexPath:indexPath];


    } else {

        [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}



- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Check that a new transition has been requested to the DetailViewController and prepares for it

        /*if ([segue.identifier isEqualToString:@"Dettaglio"]){

        // Capture the object (e.g. exam) the user has selected from the list
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        PFObject *object = [self.objects objectAtIndex:indexPath.row];
        DettagliProfilo *detailViewController = [segue destinationViewController];
        detailViewController.Dettaglio = object;*/

    if ([[segue identifier]isEqualToString:@"Dettaglio"])
    {
        DettagliProfilo *sdvc = (DettagliProfilo *)[segue destinationViewController];


        if(self.searchDisplayController.active) {
            NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
            PFObject *object = [self.objects objectAtIndex:indexPath.row];

            object = (PFObject *)[[self searchResults]objectAtIndex:[[[[self searchDisplayController]searchResultsTableView]indexPathForSelectedRow]row]];
            sdvc.Dettaglio = object;

        }   else {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            PFObject *object = [self.objects objectAtIndex:indexPath.row];
            DettagliProfilo *detailViewController = [segue destinationViewController];
            detailViewController.Dettaglio = object;


        }

    } 
}
@end
4

0 に答える 0