首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollection视图详细视图

UICollection视图详细视图
EN

Stack Overflow用户
提问于 2013-08-09 00:00:12
回答 1查看 1.7K关注 0票数 2

我的问题是这个。我有收藏视图,它能工作。不过,我不知道如何将此信息传递到详细信息页。我尝试过“prepareforsegue”方法,创建了一个浏览器类,但是不知道如何从创建的Array获得要传递给视图控制器的信息。请给我建议。

代码语言:javascript
复制
@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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-09 01:44:07

你不能从手机里得到价值。单元格只用于显示数据--您从不查询单元格的数据,而是查询用于填充单元格的数据源。要做到这一点,您可以获得发送方的indexPath:

代码语言:javascript
复制
- (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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18138215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档