【发布时间】: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