Question

My problem is this. I have the collection view, it works. I do not however know how to get this information passed to the details page. I have tried the 'prepareforsegue' method, created a browser class but have no clue how to get the information to be passed to the view controller from the Array created. Please advise.

@interface MainCollectionViewController ()

@end

@implementation MainCollectionViewController

@synthesize menubtn;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [self.collectionView addSubview:refreshControl];

    // Do any additional setup after loading the view.
    NSString *fileName = [NSString stringWithFormat:@"%@/main.rss", [MAINUtil getDocumentRoot]];
    _articleListmain = [NSArray arrayWithContentsOfFile:fileName];

    // Do any additional setup after loading the view.

    //MENU
    self.view.layer.shadowOpacity =0.75f;
    self.view.layer.shadowRadius = 10.0f;
    self.view.layer.shadowColor = [UIColor blackColor].CGColor;

    if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]])
    {
        self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
    }

    [self.view addGestureRecognizer:self.slidingViewController.panGesture];

    self.menubtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menubtn.frame = CGRectMake(20, 0, 49, 54);
    [menubtn setBackgroundImage:[UIImage imageNamed:@"menuButton.png"] forState:UIControlStateNormal];
    [menubtn addTarget:self action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.menubtn];

}

#pragma mark - UICollectionViewDataSourceDelegate
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_articleListmain count];
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"articleCellmain"
                                                                               forIndexPath:indexPath];
    NSDictionary *item = [_articleListmain objectAtIndex:indexPath.item];

    // retrieve the URL of user's avatar and assign it to the UIImageView
    NSURL *userImgURL = [NSURL URLWithString:[item objectForKey:@"image"]];
    NSData *userImgData = [NSData dataWithContentsOfURL:userImgURL];
    UIImage *userImage = [UIImage imageWithData:userImgData];
    [cell.image setImage:userImage];

    // set the text of title UILabel
    cell.title.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"title"]];

    // set the text of summary UILabel
    cell.description.text = [NSString stringWithFormat:@"%@\n\n\n\n\n\n\n\n\n", [item objectForKey:@"description"]];

    cell.targetURL = [item objectForKey:@"link"];

    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        UIImage *headerImage = [UIImage imageNamed:@"jol_logo.png"];

        reusableview = headerView;
    }

    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

        reusableview = footerview;
    }

    return reusableview;
}

- (IBAction)revealMenu:(id)sender

{
    [self.slidingViewController anchorTopViewTo:ECRight];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    MainCollectionViewCell *selectedCell = (MainCollectionViewCell *)sender;
    MainBrowserViewController *targetVC = (MainBrowserViewController *) [segue destinationViewController];
    targetVC.targetURL  = selectedCell.targetURL;
}


@end
Was it helpful?

Solution

You don't get the value from the cell. Cells are for displaying data only -- you never query a cell for its data, you query the data source that you use to populate the cell. To do that you can get the indexPath of the sender:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MainCollectionViewCell *)sender
{
     MainBrowserViewController *targetVC = (MainBrowserViewController *) [segue destinationViewController];
     NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
     NSDictionary *item = [_articleListmain objectAtIndex:indexPath.item];
     NSURL *targetURL = [item objectForKey:@"link"];
     targetVC.targetURL  = targetURL;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top