当有人在 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/