iphone - 更改 Collection View 的 UILabel 内幕 header

标签 iphone ios objective-c ios5

当有人在 Collection View (大约 15 个单元格)中选择一个单元格时

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

方法我试图通过使用更改我放置在一个标题中的标签

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];

    UILabel *headerTitle=(UILabel *)[headerView viewWithTag:1];

    headerTitle.text=@"test";

标签和所有设置都正确,但不会改变。对我哪里出错有任何想法吗?

最佳答案

您已经有一个标题 View ,因此将一个 View 出列会创建另一个 View ,而这不是您要更改的 View 。无法访问 Collection View 中的标题 View ——没有 headerForSection: 方法,因此您必须通过数据源更改标签。因此,如果您只有一个标题,那么您应该有一个字符串属性,我们称它为 headerTitle,用于填充标题中的标签。因此,您对 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 的实现应该如下所示:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    RDReusableHeader *headerView;
    if (kind == UICollectionElementKindSectionHeader){
        headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyView" forIndexPath:indexPath];
        headerView.label.text = self.headerTitle;
    }
    return headerView;
} 

然后在 didSelectItemAtIndexPath: 中,为该属性分配一个新值并在 Collection View 上调用 reloadData。

    self.headerTitle = @"This is the New Tilte";
    [self.collectionView reloadData];

关于iphone - 更改 Collection View 的 UILabel 内幕 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18238669/

相关文章:

iphone - 如何添加 uiimagepickerview 自定义覆盖

objective-c - iPhone构建编程

iphone - 核心数据 - 如何获取具有最大值属性的实体

iphone - 在正确的时间显示键盘 iOS7

ios - 在iOS中禁用tableview中的多选单元格

ios - 框架 : Why is iPhone's 4 frame discontinued?

iphone - iOS:UIImage EXC_BAD_ACCESS

iphone - 试图了解iOS平台上的内存管理

ios - 将 View Controller 推到导航 Controller 上时旋转无法正确调整插入的 View Controller 的大小

ios - 在 SceneKit 游戏中为 SCNNode 设置动画 SCNConstraint (LookAt) 以使过渡渐进