2015-12-09 13 views
2

我想設置收集視圖的標題,並因此實施方法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次,每次按照順序打印01

這與這個問題有關嗎?

回答

1

您在每次出列時都會在標題視圖中添加一個新標籤。您應該在標題中包含標籤(作爲原型,xib文件或在初始化自定義標題類時創建),並且每次只需在問題的方法中設置文本即可。

如果你不能這樣做,你需要在創建它之前檢查標籤的存在。

-1

案例1

我想你只需要從服務器獲取響應後重新加載集合視圖。並初始化視圖中的響應/ tableData數組將出現。

案例2

可以與從服務器在開關狀態響應數組數據嘗試。