如何从UIViewController访问UICollectionViewCell的IBOutlets?

时间:2014-06-06 10:39:46

标签: ios iphone objective-c uicollectionview uicollectionviewcell

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

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

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

修改

这就是我试图从cellForRowAtIndexPath更改其中一个标签但它没有改变的方式。

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

1 个答案:

答案 0 :(得分:0)

如果在其xib中映射TopCell,您所要做的就是引用其属性。如果你想自定义,你需要覆盖CCSticky ...并在你的xib和这个类中引用它。