2016-12-15 51 views
-2
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell 



    //let imageView = UIImageView() 
    //cell.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00) 

    cell.layer.borderWidth = 0.5 
    cell.layer.borderColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor 


    //cell.placeLabel.tintColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor 

    cell.layer.cornerRadius = 40 
    cell.layer.masksToBounds = true 


    print("places\(indexPath.row)") 
    //cell.placeLabel.text = places[indexPath.row] as! String 
    cell.placeLabel.text = places[indexPath.row] 
    cell.placeLabel.textColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00) 

    // code for VIew 
    let view = UIView(frame: cell.bounds) 
    //Set background color that you want 
    view.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00) 
    cell.selectedBackgroundView = view 



    return cell 

我在集合視圖單元格中有一個圖像。我只需要單擊單元格時顯示該圖像。我們必須在cellforRowat索引路徑中執行此操作嗎?如何在UICollection View單元中顯示圖像,同時單擊swift ios中的單元格?

//Custom class code: 


    @IBOutlet weak var tickImageView: UIImageView! 

@IBOutlet weak var placeViewBtn: UIButton! 
@IBOutlet weak var placeLabel: UILabel! 

@IBOutlet weak var imageView: UIImageView! 
override func layoutSubviews() { 

} 
    override func awakeFromNib() { 

    self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2 

    self.imageView.layer.masksToBounds = true 
    super.awakeFromNib() 
    // Initialization code 
} 

添加了自定義類代碼。我想要顯示上面已經聲明的ticMark圖像。

+0

你必須使用didSelectRowAtIndexPath方法方法 –

+0

你說的意思是「我在收集觀察單元的圖像。我只需要顯示圖像時,我點擊單元格。」? –

+0

@NiravD其實它是一個「嘀嗒」的圖像。當我點擊collectionview中的一個單元格時,該tickmark圖像應該只顯示在該單元格上。 –

回答

2

聲明一個IndexPath類型的實例並保持其單元狀態爲selected或不與它。現在使用cellForItemAt indexPathdidSelectItemAt indexPath這個數組。

var selectedIndexPaths = IndexPath() 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell 
    //Your code 

    //decide weather to show tick image or not 
    self.tickImageView.isHidden = self.selectedIndexPaths != indexPath 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    if self.selectedIndexPaths == indexPath { 
     self.selectedIndexPaths = IndexPath() 
    } 
    else { 
     self.selectedIndexPaths = indexPath 
    } 
    self.tableview.reloadData() 
} 
+0

我必須在我選擇的單元格上顯示它。現在它顯示在我選擇的所有單元上。 –

+0

@RakeshMohan你想在一次只有一個單元格的選擇? –

+0

是的。我只需要在我選擇的單元格中顯示該圖像。 –

1

您需要閹羊蜱在cellForRow

選擇或不讀,如果打勾選擇了{ showImage }其他{ hideImage }

然後在didSelectRow 設置打勾選擇或取消選中 然後調用reloadRowForIndexPath

+0

你能告訴我如何在我的代碼中執行它嗎? –

相關問題