如何从UIViewController访问UICollectionViewCell的IBOutlet? [英] How to access IBOutlets of a UICollectionViewCell from a UIViewController?

查看:53
本文介绍了如何从UIViewController访问UICollectionViewCell的IBOutlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIViewController内实现了UICollectionView.标题单元格是在单独的.xib文件中创建的,并通过IBOutlets和IBAction在单独的.h和.m文件中实现.其余单元在同一UIVIewController中实现(原因是因为我添加了视差效果).

I implemented a UICollectionView inside a UIViewController. The header cell is created in a separate.xib file and it is implemented in a separate .h and .m files with the IBOutlets and IBActions. The rest of the cells are implemented in the same UIVIewController (The reason is because I added this Parallax effect).

我想从包含collectionview的viewcontroller(RankingViewController)修改标头单元(CSCellUser.h)中的IBoutlet(标签和按钮)的信息,我该怎么做?

I would like to modify the info of the IBoutlets (labels and buttons) that are in the header cell (CSCellUser.h) from the viewcontroller (RankingViewController) which containts the collectionview, how can I do this?

Cell:CSCellUser.h

Cell: CSCellUser.h

@interface CSCellUser : UICollectionViewCell

@property IBOutlet UILabel *myScoreValueLabel;
@property IBOutlet UILabel *myRankingValueLabel;

-(IBAction) sendButtonTouchHandler:(id) sender;

@end

UIViewController:RankingViewController.h

UIViewController: RankingViewController.h

@interface RankingViewController : CommonViewController <UICollectionViewDelegate, UICollectionViewDataSource> {
}

@property IBOutlet UICollectionView *collectionView1;

@end

UIViewController:RankingViewController.m

UIViewController:RankingViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Parallax Effect, UICollectionView
    // Locate the layout
    CSStickyHeaderFlowLayout *layout = (id)self.collectionView1.collectionViewLayout;
    if ([layout isKindOfClass:[CSStickyHeaderFlowLayout class]]) {
        layout.parallaxHeaderReferenceSize = CGSizeMake(320, 220);
        layout.parallaxHeaderAlwaysOnTop = YES;
    }

    // Locate the nib and register it to your collection view
    UINib *headerNib = [UINib nibWithNibName:@"CSHeaderRanking" bundle:nil];
    [self.collectionView1 registerNib:headerNib
           forSupplementaryViewOfKind:CSStickyHeaderParallaxHeader
                  withReuseIdentifier:@"TopViewCell"];

    //get the position of the user and the ranking (this should update the IBOutlets in the CSCellUser.h)
    [self getUserRanking];

    //get the ranking of users (this updates each cell of the ranking in cellForItemAtIndexPath) 
    [self getRanking];



}



- (NSInteger) numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
    return 1;
}

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

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
                   cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"UsersCell" forIndexPath:indexPath];

        if ([ranking count] > 0)
        {
            UserRanking *user = [ranking objectAtIndex:indexPath.row];  //Fill the cell

            UILabel *usernameLabel = (UILabel *)[cell viewWithTag:101];
            usernameLabel.text = user.username;

            UILabel *scoreLabel = (UILabel *)[cell viewWithTag:102];
            scoreLabel.text = [NSString stringWithFormat:@"%d", user.score];

            UILabel *gamesLabel = (UILabel *)[cell viewWithTag:103];
            gamesLabel.text =[NSString stringWithFormat:@"%d", user.tries];

    }
    return cell;
}

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

    if ([kind isEqualToString:CSStickyHeaderParallaxHeader]) {
        reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"TopViewCell" forIndexPath:indexPath];

    }


    return reusableview;
}

这是我尝试从cellForRowAtIndexPath更改标签之一,但它没有更改的方式.

This is how I am trying to change one of the labels from cellForRowAtIndexPath but it does not change.

  CSHeaderRanking *topcell = [collectionView dequeueReusableCellWithReuseIdentifier: @"TopCell" forIndexPath:indexPath];
    topcell.myScoreValueLabel.text = @"32";

推荐答案

如果TopCell映射在其xib中,那么您只需参考其属性即可.如果要自定义,则需要重写CCSticky ...,并在xib和此类中引用它.

If TopCell is mapped in its xib, all you have to do is refer to its properties. If you want to customize, you'll need to over-ride the CCSticky... and refer to it in your xib and this class.

这篇关于如何从UIViewController访问UICollectionViewCell的IBOutlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆