如何在单独的.xib文件中更新自定义UICollectionViewCell的IBOutlets?

时间:2014-06-11 11:17:25

标签: ios objective-c uiviewcontroller uicollectionview uicollectionviewcell

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

我想从viewcontroller修改TopCell中的IBOutlets(标签和按钮)的信息。这是我试图修改但它不起作用的方式(我在cellForRowAtIndexPath和viewForSupplementaryElementOfKind中尝试过)。

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

我该怎么办?我应该在哪种方法中添加它?

这是我的viewcontroller:

- (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"];
    [self.collectionView1 registerClass:[CSHeaderRanking class] forCellWithReuseIdentifier:@"TopCell"];


    //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];

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

    return cell;
}

//Set TopViewCell
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{        
    if ([kind isEqualToString:CSStickyHeaderParallaxHeader]) {
       UICollectionReusableView  *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"TopViewCell" forIndexPath:indexPath];
        return reusableview;
    }

    return nil;
}

0 个答案:

没有答案