我想設置收集視圖的標題,並因此實施方法func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
。爲什麼「switch」語句在設置UICollectionView標題時不起作用?
但是,看來開關聲明不起作用;即使當我試圖通過分支取決於部分來設置標題視圖內的標籤時,所有部分中的合成標題視圖也具有我寫的所有標籤。
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath)
let headerLabel = UILabel(frame: CGRectMake(2, 8, 120, 24))
headerView.addSubview(headerLabel)
print(indexPath.section)
switch (indexPath.section) {
case 0:
headerLabel.text = "A"
return headerView
case 1:
headerLabel.text = "B"
return headerView
default:
break
}
return headerView
default:
assert(false, "Unexpected element kind")
}
}
在上面的代碼中,這兩個部分標籤具有兩個標籤甲和乙,相互重疊。
爲什麼交換機在我的情況下不起作用?
我的收藏視圖中的內容從服務器獲取數據,因此print(indexPath.section)
被執行2次,每次按照順序打印0
和1
。
這與這個問題有關嗎?