Ios UICollectionView`reloadData`导致UILabel行消失
我在Ios UICollectionView`reloadData`导致UILabel行消失,ios,uicollectionview,uilabel,reloaddata,Ios,Uicollectionview,Uilabel,Reloaddata,我在collectionView标题中有一个ui标签。标签设置为零行、换行和适当的前导/尾随/顶部空间约束。如果我不调用[collectionView reloadData],标签将正确扩展为两行以上的文本。调用reloadData后,标签返回到一行……第二行消失 - (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(
collectionView
标题中有一个ui标签。标签设置为零行、换行和适当的前导/尾随/顶部空间约束。如果我不调用[collectionView reloadData]
,标签将正确扩展为两行以上的文本。调用reloadData
后,标签返回到一行……第二行消失
- (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;
}
collectionView:viewforsupplementalelementofkind:atIndexPath:
用于整个collectionView的页眉或页脚,而collectionView:referenceSizeForHeaderInstitution:
用于collectionView特定部分的页眉
要确保标头的大小保持一致并尊重您的约束,您需要调用[(UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout setHeaderReferenceSize:CGSizeMake(宽度、高度)]
流程布局实例化后collectionView:ViewForSupplementalElementOfKind:atIndexPath:
用于整个collectionView的页眉或页脚,而collectionView:ReferenceSizeForHeaderInstitution:
用于collectionView特定部分的页眉
要确保标头的大小保持一致并尊重您的约束,您需要调用
[(UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout setHeaderReferenceSize:CGSizeMake(宽度、高度)]
一旦flowlayout被实例化在委托方法中使用label setter属性,在委托方法中配置每个单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//add label property here
return cell;
}
重新加载后,它再次调用此方法来配置标签 在委托方法中使用标签设置器属性,在委托方法中配置每个单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//add label property here
return cell;
}
重新加载后,它再次调用此方法来配置标签 尝试将preferredMaxLayoutWidth设置为标签
self.label.preferredMaxLayoutWidth = 300;
尝试将preferredMaxLayoutWidth设置为标签
self.label.preferredMaxLayoutWidth = 300;
请发布代码。@Ismail它是在情节提要中完成的。只有代码是重新加载的数据您如何处理故事板中
UICollectionView
的所有内容???数据源/委托方法在哪里?@Ismail这是我的头方法。你找到解决方案了吗?我遇到了完全相同的问题。请发布代码。@Ismail它是在故事板中完成的。只有代码是重新加载的数据您如何处理故事板中UICollectionView
的所有内容???数据源/委托方法在哪里?@Ismail这是我的头方法。你找到解决方案了吗?我遇到了完全相同的问题。重新加载数据后,标签仍然返回到一行。这一定是有限制的东西。奇怪的是,你想把标签直接贴出来的是@“title”
还是@“info”
?另外,174.0f
参考了什么?@adulfo174.0f
是收割台的原始高度。首先,我试图将高度扩展到rollTitle
,因此我返回原始页眉高度+扩展到@“title”
@Peter的标签高度。我不确定是什么导致了问题。我的直觉是,在创建labelRect
的地方放置一个断点,然后检查header.rollTitle.frame.size
,查看正在传递的boundingrectizewithsize
也不需要调用reloadData
,只需滚动collectionView
三行标签就会返回到一行。不确定这是否会为不同的潜在问题提供标志。重新加载数据后,标签仍会返回到一行。这一定是有限制的东西。奇怪的是,你想把标签直接贴出来的是@“title”
还是@“info”
?另外,174.0f
参考了什么?@adulfo174.0f
是收割台的原始高度。首先,我试图将高度扩展到rollTitle
,因此我返回原始页眉高度+扩展到@“title”
@Peter的标签高度。我不确定是什么导致了问题。我的直觉是,在创建labelRect
的地方放置一个断点,然后检查header.rollTitle.frame.size
,查看正在传递的boundingrectizewithsize
也不需要调用reloadData
,只需滚动collectionView
三行标签就会返回到一行。不确定这是否为您提供了另一个潜在问题的标志。