如何从UIViewController访问UICollectionViewCell的IBOutlets?

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

我想从包含collectionview的视图控制器(RankingViewController)修改标题单元格(CSCellUser.h)中的IBoutlets(标签和button)的信息,我该如何做到这一点?

单元格: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"; 

如果TopCell被映射到它的xib,你所要做的就是引用它的属性。 如果你想定制,你需要重新CCSticky …并在你的xib和这个类中引用它。