Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

obey me
obey me
1,135 Points

Passing data From UITableView To UIViewcontroller which contains a UICollectionView

Stone Preston need your help man !!! please

//DOG.h

   @property(nonatomic,strong) NSString *stringdata;
   @property (nonatomic,strong) IBOulet UILabel *dogname;

 @property(nonatomic, strong )  IBOulet UICollectionView *dogcollection;
 @property (nonatomic,strong)    NSArray *array;

// DOG.m

 @synthesize  stringdata;
 @synthesize  dogname;

    -(void)viewDidLoad{

       dogname.text=stringdata;

      array=[[NSArray alloc]init];

       [super viewDidLoad];
       }


    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {

        return array.count;
      }


         - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
      {

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



        - (UICollectionView *)collectionView:(UICollectionView *)collectionView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
    static NSString *CellIndentifier=@"imagecell";
   CollectionCell *cell =(CollectionCell*) [collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath];


    return cell;
  }

//-------------------------------------------------------------------------------

The tableview contain a list of dog types, to pass data I used the segue "showdogsegue"

      -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if([segue.identifier isEqualToString:@"showdogsegue"])
     {
       NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
       DOG *details= (DOG*)segue.destinationViewController;

       PFObject *object=[self.typeDog objectAtIndex:indexPath.row ];
       details.stringdata=[object objectForKey:@"username"];  

  details.dogcollection=[object objectForKey:@"Post_image"];// that's where i am trying to pass the data to the collectionview 

        }

  }

3 Answers

Stone Preston
Stone Preston
42,016 Points

you have the right idea. However you dont need to modify the collection view directly. you implement collection view delegate methods (which you did) and those methods use an array data source to get the data from (which they do, they use the array property you declared in dog.h). you need to set your array property

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if([segue.identifier isEqualToString:@"showdogsegue"])
     {
       NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
       DOG *details= (DOG*)segue.destinationViewController;

       PFObject *object=[self.typeDog objectAtIndex:indexPath.row ];
       details.stringdata=[object objectForKey:@"username"];  

      // pass whatever data you want to display in the collection view as an array here.
      details.array = ...

        }

  }

you may want to change the name of the array property to something more descriptive. also, in your cellForRowAtIndexpath method you will need to use the array to set the data displayed in the cell. if your cell has an image view or something thats where you would set that up. but that all depends on how you set your cell up

 - (UICollectionView *)collectionView:(UICollectionView *)collectionView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
    static NSString *CellIndentifier=@"imagecell";
   CollectionCell *cell =(CollectionCell*) [collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath];
//set the cell properties here. did you add a lable or imageView to your cell? here is where you set that
cell.imageView.image = [self.array objectAtIndex:indexPath.row]

    return cell;
  }
obey me
obey me
1,135 Points

so i dont need to set up anything in the viewDidLoad

Stone Preston
Stone Preston
42,016 Points

you might. depends on what you want to do haha

obey me
obey me
1,135 Points

i did what you said there is no image in the cell i try to use

         [self.collectionview reloadData];

// should I load the uicollection with the array

Stone Preston

obey me
obey me
1,135 Points

Kai Aldag help in this line

Gotcha ill take a look at this once i arrive home in like 2 hours or so.

obey me
obey me
1,135 Points

in my viewdidload can i just pass the array to the uicollectionview

Yes, but if you need to make a network call to get the array then you must wait to get the a data back to pass in the array. But you can simply refresh in the completion block.

obey me
obey me
1,135 Points

i used to Stone Preston answers but when the page load there is no images in the uitablewiewcell

Ok I'll check it out

OK first off, you need to register the cells reuseIdentifier in viewDidLoad,

    [self.tableView registerClass:[UITableViewCell class] forCellWithReuseIdentifier:@"imagecell"];

so, if we're passing data we can simply pass in the data in the prepare for segue method to the next viewController, and the way we can do this is to pass some object from the tableView to a property in the dog.h viewController.

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if([segue.identifier isEqualToString:@"showdogsegue"])
     {
       NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
       DOG *details= (DOG*)segue.destinationViewController;

       PFObject *object=[self.typeDog objectAtIndex:indexPath.row ];
       details.stringdata=[object objectForKey:@"username"];  

      //now here we pass in what ever data we want in the next viewController so if we have an array called dogs for the next viewController we can do something like

        details.array = /* someObject that you want to pass in*/
        }

  }

also, what is the problem that I'm looking for? are we finding in issue or simply passing data between viewControllers?

Kai

obey me
obey me
1,135 Points

i did what you told me ,still no images were displayed in the collectionview

do you have a git repository that i can check out? if not can you make a .zip file so i can head in and debug it?