【发布时间】:2017-07-19 07:56:52
【问题描述】:
CollectionView 具有标题和自定义布局。 当我单击单元格时,有两种方法正在工作。 1)didSelectItemAt 2) viewforsupplementaryelementofkind 我单击单元格并滚动到页面的开头。 无法动态检测 header 中的 detailPhotoStory 大小。
var headerHeight: CGFloat = 0.0
var headerView: DetailCollectionReusableView?
//viewForSupplementaryElementOfKind
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) ->UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader
{
headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader,
withReuseIdentifier: "detailCollectionReusableView",
for: indexPath) as? DetailCollectionReusableView
headerView?.detailPhotoStory.text = PhotoItemArraySelected[indexPath.row].photoStory
headerView?.setNeedsLayout()
headerView?.layoutIfNeeded()
headerHeight = 380
headerHeight += (headerView?.detailPhotoStory.frame.height)!
return headerView!
}
return UICollectionReusableView()
}
//didSelectItemAt
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
headerView?.detailPhotoStory.text = PhotoItemArraySelected[0].photoStory
headerView?.setNeedsLayout()
headerView?.layoutIfNeeded()
headerHeight = 380
headerHeight += (headerView?.detailPhotoStory.frame.height)!
collectionView.reloadData()
collectionView.scrollToItem(at: IndexPath(row: 0, section: 0),
at: .top,
animated: true)
}
【问题讨论】:
-
请更具体地说明您要询问的内容。您指的是哪种标签尺寸?
-
使用这个referenceSizeForHeaderInSection,从你的内容中获取高度并返回你的高度
-
@karthikeyan 我使用了referenceSizeForHeaderInSection,但collectionview的标题没有改变。
标签: ios swift uicollectionview uicollectionviewcell collectionview