2017-06-22 42 views
0

我正在使用集合視圖爲我的醫院構建iOS應用程序。但是,我需要爲專科診所使用多個部分取決於目的。如果只是1部分,我已經完成了代碼。當我試圖將它分成兩部分時,它總是返回一個零值。UICollectionView中的多個部分

請檢查我的代碼如下

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 

    if indexPath.section == 0 { 
     var buttonSpecialist = cell.viewWithTag(1) as! UIButton 

     buttonSpecialist.setTitle(list[indexPath.row].SpecialtyName, for: .normal) 

     let btnimg = try? UIImage(data: Data(contentsOf: URL(string: list[indexPath.row].ImageUrl)!)) 

     var newimg = imageResize(image: btnimg as! UIImage, scaledTo: CGSize(width: 50, height: 50)) 

     buttonSpecialist.setImage(newimg, for: .normal) 
     buttonSpecialist.contentMode = .center 
     buttonSpecialist.imageView?.contentMode = .scaleAspectFit 

     //let sign: Int = imageOnTop ? 1 : -1 
     let imageSize: CGSize? = buttonSpecialist.imageView?.frame.size 
     buttonSpecialist.titleEdgeInsets = UIEdgeInsetsMake(((imageSize?.height)! + 5) * 1, -(imageSize?.width)!, 0, 0) 
     let titleSize: CGSize? = buttonSpecialist.titleLabel?.bounds.size 
     buttonSpecialist.imageEdgeInsets = UIEdgeInsetsMake(-((titleSize?.height)! + 5) * 1, 0, 0, -(titleSize?.width)!) 

     cell.layer.borderWidth = 1.0 
     cell.layer.borderColor = UIColor.black.cgColor 
    } 
    else if indexPath.section == 1 { 
     var buttonSpecialist = cell.viewWithTag(1) as! UIButton 

     buttonSpecialist.setTitle(list2[indexPath.row].SpecialtyName, for: .normal) 

     let btnimg = try? UIImage(data: Data(contentsOf: URL(string: list2[indexPath.row].ImageUrl)!)) 

     var newimg = imageResize(image: btnimg as! UIImage, scaledTo: CGSize(width: 50, height: 50)) 

     buttonSpecialist.setImage(newimg, for: .normal) 
     buttonSpecialist.contentMode = .center 
     buttonSpecialist.imageView?.contentMode = .scaleAspectFit 

     //let sign: Int = imageOnTop ? 1 : -1 
     let imageSize: CGSize? = buttonSpecialist.imageView?.frame.size 
     buttonSpecialist.titleEdgeInsets = UIEdgeInsetsMake(((imageSize?.height)! + 5) * 1, -(imageSize?.width)!, 0, 0) 
     let titleSize: CGSize? = buttonSpecialist.titleLabel?.bounds.size 
     buttonSpecialist.imageEdgeInsets = UIEdgeInsetsMake(-((titleSize?.height)! + 5) * 1, 0, 0, -(titleSize?.width)!) 

     cell.layer.borderWidth = 1.0 
     cell.layer.borderColor = UIColor.black.cgColor 
    } 

主要的問題在這裏,我不知道如何設置特定小區的特定部分。我已經在函數'cellforindexpath'中使用'IF',但它沒有奏效。

這是部分項目我回數

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 2 
} 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of items 

    if section == 0 { 
     return list.count 
    } 
     else if section == 1{ 
      return list2.count 
     } 

    return 0 

} 

請幫我

+0

附加numberofSection方法。 – KKRocks

+0

您在方法numberOfSections(in :)中返回的值是多少?你確定條件「indexPath.section == 1」的代碼段實際執行嗎? – alanlo

+0

@ christ2702你是否將單元格當作零? –

回答

0

確保分配的DataSource &委派到的CollectionView

確定公司運作的數量的部分你想用下面的方法顯示

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 2 
} 

2.使用下面的方法爲CollectionView設置每個部分的項目數。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return (section == 0) ? list.count : list2.count 
} 

3.將每個項目的單元格分配給CollectionView。

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    //If you are using multiple cells based on section make condition 

    if indexPath.section == 0 { 
      //make sure the identifier of your cell for first section 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) 
      // do your stuffs 
      return cell 
    }else{ 
      //make sure the identifier of your cell for second section 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) 
      // do your stuffs 
      return cell 
     } 

} 
+0

我想要相同的單元格,但單元格內的內容不同 – christ2702

+0

@ christ2702然後只需在頂部方法中放置let cell = collectionView.dequeueReusableCell(withReuseIdentifier:「cell1」,for:indexPath) 。 –

+0

還有1個問題。如何在每個部分中添加節標題? – christ2702

0

如果在這種情況下,你應該使用不同的重用標識不同的定製單元不同的單元佈局的要求。正如我在你的代碼中看到的,你有不同數據源的單元格佈局。

所以我會說嘗試subclass的collectionview單元格,並把按鈕的IBOutlet屬性通過xib或storyboard在子類中。

你可以參考這個問題: how to create custom UICollectionViewCell