How to access IBOutlets of a UICollectionViewCell

2019-07-27 01:39发布

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).

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

@interface CSCellUser : UICollectionViewCell

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

-(IBAction) sendButtonTouchHandler:(id) sender;

@end

UIViewController: RankingViewController.h

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

@property IBOutlet UICollectionView *collectionView1;

@end

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

EDIT:

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";

1条回答
三岁会撩人
2楼-- · 2019-07-27 02:30

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.

查看更多
登录 后发表回答