如何从一个UIViewController访问UICollectionViewCell的IBOutl

2019-10-20 06:17发布

我实现了一个UIViewController内UICollectionView。 标题单元格是在separate.xib文件创建,它是在一个单独的h和.m文件与IBOutlets和IBActions实现。 细胞的其余部分都在同一个UIViewController中实现(原因是因为我加入这个视差效果)。

我想修改是从containts的的CollectionView的视图 - 控制(RankingViewController)头单元格(CSCellUser.h)的IBoutlets(标签和按钮)的信息,我该怎么办呢?

手机: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;
}

编辑:

这是我正在试图改变从的cellForRowAtIndexPath标签之一,但它不会改变。

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

Answer 1:

如果TOPCELL在其厦门国际银行映射,所有你需要做的是指它的属性。 如果你想定制,你需要为忽略的CCSticky ...并指它在你的厦门国际银行和这个类。



文章来源: How to access IBOutlets of a UICollectionViewCell from a UIViewController?