2016-01-22 50 views
1

我在collectionView头中有UILabel。该标签设置为零线,文字换行和正确的前导/尾随/顶部空间约束。如果我不打电话给[collectionView reloadData],标签将正确扩展为超过两行的文字。一旦调用reloadData,标签就会回到单行......第二行消失。UICollectionView`reloadData`会导致UILabel行消失

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ 
    if (kind == UICollectionElementKindSectionHeader) { 
     header = (viewRollHeader *) [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; 
     header.rollTitle.text = [self.roll objectForKey:@"title"]; 
     header.rollDescription.text = [self.roll objectForKey:@"info"]; 

     [header.cancelButton addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside]; 

     return header; 
    } 
    return [UICollectionReusableView new]; 
} 


- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ 
    if (section == 0) { 
     CGRect labelRect = [[self.roll objectForKey:@"title"] 
         boundingRectWithSize: header.rollTitle.frame.size 
         options:NSStringDrawingUsesLineFragmentOrigin 
         attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Arial-BoldMT" size:32.0f]} 
         context:nil]; 
     return CGSizeMake([[UIScreen mainScreen]bounds].size.width, (174.0f + labelRect.size.height)); 
    } 
    return CGSizeZero; 
} 
+0

邮政代码,请。 – Ismail

+0

@Ismail在故事板中完成。只有代码是reloadData – Peter

+3

如何处理故事板中的“UICollectionView”的所有内容?数据源/委托方法在哪里? – Ismail

回答

0

collectionView:viewForSupplementaryElementOfKind:atIndexPath:用于整个的CollectionView的页眉或页脚,而collectionView:referenceSizeForHeaderInSection:用于的CollectionView

的特定的报头,以确保报头的大小保持一致并尊重您的约束,一旦实例化流布局,您需要致电[(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout setHeaderReferenceSize:CGSizeMake(width, height)]

+0

reloadData后标签仍然回到一行。它必须是有约束力的东西。 – Peter

+0

好奇 - 它是“@”标题“'还是'@”信息“'你的意思是将标签基于?另外,'174.0f'是什么引用的? – aduflo

+0

@adulfo'174.0f'是标题的原始高度。首先,我试图将高度扩展到'rollTitle'因此,我将原始标题高度+标签的高度扩展为'@“title”' – Peter

0

在委托方法内使用标签设置器属性,绘制每个单元格。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 
//add label property here 
    return cell; 
} 

重新加载后,它再次调用此方法来配置标签。

0

尝试preferredMaxLayoutWidth设置标签

self.label.preferredMaxLayoutWidth = 300;