UICollection查看详细信息视图

时间:2013-08-09 00:00:13

标签: ios xml-parsing rss-reader

我的问题是这个。我有集合视图,它的工作原理。但是,我不知道如何将此信息传递到详细信息页面。我尝试了'prepareforsegue'方法,创建了一个浏览器类,但不知道如何从创建的数组中将信息传递给视图控制器。请指教。

@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

1 个答案:

答案 0 :(得分:2)

您没有从单元格中获取值。单元格仅用于显示数据 - 您永远不会在单元格中查询其数据,您查询用于填充单元格的数据源。为此,您可以获取发件人的indexPath:

- (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;
}