【问题标题】:How to access IBOutlets of a UICollectionViewCell from a UIViewController?如何从 UIViewController 访问 UICollectionViewCell 的 IBOutlets?
【发布时间】:2014-06-06 10:39:45
【问题描述】:

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

我想从包含集合视图的视图控制器 (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";

【问题讨论】:

  • 将 UICollectionViewCell 替换为您的自定义集合单元 (CSCellUser)。
  • @pawan 你的意思是在 cellForItemAtIndexPath 中吗?我应该复制该方法吗?我使用 CSCellUser 作为标题,使用 UICollectionViewCell 作为其余单元格(它就像一个表格视图)。

标签: ios iphone objective-c uicollectionview uicollectionviewcell


【解决方案1】:

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

【讨论】:

  • 我尝试从 cellForRowAtIndexPath 访问它的属性,但它没有做任何事情(见编辑)。我是否需要覆盖 CCSticky..?
  • 你能给我一个例子,你会怎么做?由于从 ViewController 引用属性(我需要从 viewcontroller 执行,因为我从 json 文件中获取数据)不起作用我想我应该覆盖 CCStickyHeader 但你到底是什么意思?我应该重写哪些方法?
猜你喜欢
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多