Question

Can someone please help me, I looked everywhere to figure this out and nothing worked so far. I need to pass some data from table view to detail view and stick it into labels and Uiimage.

Data tableview is pulling comes from Parse database I created and seems to get pulled fine into the Tableview but I would like to use the same array that tableview is using for its data to fill out the detail view.

I am using 2 columns from parse to fill out this tableview Title and sub, and another tow columns to fill out the label and image. Here is my code so far. There is a bunch of variables that i was using in this code in DetailView

.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface BooksTableViewController : UITableViewController <UITableViewDelegate,NSObject >
{
    NSArray * Booksarray;
}
@property (strong, nonatomic) IBOutlet UITableView *bookstableview;
@end



.m

#import "BooksTableViewController.h"
#import "BookDetailViewController.h"

@interface BooksTableViewController ()

@end

@implementation BooksTableViewController

@synthesize bookstableview;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self performSelector:@selector(RetrieveDatafromParse)];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void) RetrieveDatafromParse {
    PFQuery * getbooks = [PFQuery queryWithClassName:@"BooksTableView"];

    [getbooks findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if(!error) {
            Booksarray =[[NSArray alloc] initWithArray: objects];
        }
        [bookstableview reloadData];
        NSLog(@"%@",objects);
    }];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

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

    // Return the number of rows in the section.
    return Booksarray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
UITableViewCell * cell = [bookstableview dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil) {
        cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    PFObject * tempObject = [Booksarray objectAtIndex:indexPath.row];
    cell.textLabel.text = [tempObject objectForKey:@"Books"];
    cell.detailTextLabel.text= [tempObject objectForKey:@"Code"];

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BookDetailViewController * detailVC=[[BookDetailViewController alloc] initWithNibName:@"BookDetailViewController" bundle:nil];
    detailVC.BookImage.image=[Booksarray objectAtIndex:indexPath.row];
    detailVC.bookDesc.text=[Booksarray objectAtIndex:indexPath.row];
    detailVC.bookTitle.text=[Booksarray objectAtIndex:indexPath.row];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.destinationViewController isKindOfClass: [BookDetailViewController class]]) {
        BookDetailViewController *destination = segue.destinationViewController;

        SEL selector = NSSelectorFromString(@"SetFile:");

        if ([destination respondsToSelector:selector]) {

            NSIndexPath *indexPath = [self.bookstableview indexPathForCell:sender];

            PFObject * object = [Booksarray objectAtIndex:indexPath.row];
              PFFile *file = [object objectForKey:@"BooksTableView"];
             [destination setValue:file forKey:@"file"];
        }


    }
}


@end



.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface BookDetailViewController : UIViewController <NSObject> {


}

@property (weak, nonatomic) IBOutlet UIImageView *BookImage;
@property (weak, nonatomic) IBOutlet UILabel *bookTitle;
@property (weak, nonatomic) IBOutlet UILabel *bookDesc;
@property (weak,nonatomic)PFFile *file;
@end




 .m


#import "BookDetailViewController.h"
#import "BooksTableViewController.h"
@interface BookDetailViewController ()




@implementation BookDetailViewController

@synthesize BookImage,bookTitle,bookDesc,file,bookInfo,Picture,object2;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {


        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
  [self performSelector:@selector(RetrieveObjectsFromParse)];
    self.bookTitle.text = [self.file objectForKey:@"Books"];
    self.BookImage.image = [self.file objectForKey:@"BookImage"];
    self.bookDesc.text =[self.file objectForKey:@"BookDetails"];

}


-(void)RetrieveObjectsFromParse {
    PFQuery * GetObjects = [PFQuery queryWithClassName:@"BooksTableView"];

    [GetObjects findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if(!error) {
            details =[[NSArray alloc] initWithArray: objects];
        };
        NSLog(@"%@",objects);
    }];

}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}




@end
Was it helpful?

Solution

Try this:

#1 create a segue from controller to controller:

#2 Give your segue an Id for example detailSegue.

#3 Perform the segue in didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [self performSegueWithIdentifier:@"detailSegue" sender:sender];
}

#4 Implement the segue delegate:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 // Make sure your segue name in storyboard is the same as this line
 if([segue.identifier isEqualToString:@"detailSegue"]){
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    BookDetailViewController *detailVC = (BookDetailViewController *)segue.destinationViewController;
    detailVC.bookDesc.text=[Booksarray objectAtIndex:indexPath.row];
    //I uncommented that because it looks like a typo, same value 3 times?
    //detailVC.BookImage.image=[Booksarray objectAtIndex:indexPath.row];
    //detailVC.bookTitle.text=[Booksarray objectAtIndex:indexPath.row];
 }
}

If this is the log you get:

2014-03-21 15:06:20.151 BookStore[25539:90b] BookIndex= { BookDetails = "Test test"; BookImage = ""; Books = Languages; Code = 104; }

Then you need to do it like this instead:

detailVC.bookTitle.text=[[Booksarray objectAtIndex:indexPath.row]objectForKey:@"Books"];
detailVC.bookDesc.text= [[Booksarray objectAtIndex:indexPath.row]objectForKey:@"BookDetails"];
detailVC.BookImage.image=[[Booksarray objectAtIndex:indexPath.row]objectForKey:@"BookImage"];

Or to make it shorter:

NSArray *bookAtIndex = [Booksarray objectAtIndex:indexPath.row];
detailVC.bookTitle.text=[bookAtIndex objectForKey:@"Books"];
detailVC.bookDesc.text= [bookAtIndex objectForKey:@"BookDetails"];
detailVC.BookImage.image=[bookAtIndex objectForKey:@"BookImage"];

or even shorter

NSArray *bookAtIndex = Booksarray[indexPath.row];
detailVC.bookTitle.text= bookAtIndex[@"Books"];
detailVC.bookDesc.text= bookAtIndex[@"BookDetails"];
detailVC.BookImage.image= bookAtIndex[@"BookImage"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top