2017-03-24 41 views
1

我有一個ImageView的和標籤定製UICollectionView細胞,如下所示:不能單擊自定義UICollectionViewCell

import UIKit 

class PollCell: UICollectionViewCell { 


@IBOutlet weak var imageView: UIImageView! 

@IBOutlet weak var pollQuestion: UILabel! 

} 

我要讓每個居住單元可點擊,然後從信息傳遞單擊的單元格到一個新的視圖控制器。下面是填充自定義單元格的ViewController。

import UIKit 
import FirebaseDatabase 
import FirebaseDatabaseUI 
import FirebaseStorageUI 


private let reuseIdentifier = "PollCell" 

class TrendingViewController: UICollectionViewController { 

var ref: FIRDatabaseReference! 
var dataSource: FUICollectionViewDataSource! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    ref = FIRDatabase.database().reference() 


    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Register cell classes 

    self.dataSource = self.collectionView?.bind(to: self.ref.child("Polls")) { collectionView, indexPath, snap in 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCell 
     /* populate cell */ 
     cell.pollQuestion.text = snap.childSnapshot(forPath: "question").value as! String? 
     let urlPollImage = snap.childSnapshot(forPath: "image_URL").value as! String? 
     cell.imageView.sd_setImage(with: URL(string: urlPollImage!), placeholderImage: UIImage(named: "Fan_Polls_Logo.png")) 
     //Comment 
     return cell 
    } 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
print(indexPath) 
} 


} 

我也有一些代碼爲「反轉」是填充在我倒立的ViewController細胞:

import Foundation 
import UIKit 

class InvertedStackLayout: UICollectionViewLayout { 
    let cellHeight: CGFloat = 100.00 // Your cell height here... 

override func prepare() { 
    super.prepare() 
} 

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
    var layoutAttrs = [UICollectionViewLayoutAttributes]() 

    if let collectionView = self.collectionView { 
     for section in 0 ..< collectionView.numberOfSections { 
      if let numberOfSectionItems = numberOfItemsInSection(section) { 
       for item in 0 ..< numberOfSectionItems { 
        let indexPath = IndexPath(item: item, section: section) 
        let layoutAttr = layoutAttributesForItem(at: indexPath) 

        if let layoutAttr = layoutAttr, layoutAttr.frame.intersects(rect) { 
         layoutAttrs.append(layoutAttr) 
        } 
       } 
      } 
     } 
    } 

    return layoutAttrs 
} 

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 
    let layoutAttr = UICollectionViewLayoutAttributes(forCellWith: indexPath) 
    let contentSize = self.collectionViewContentSize 

    layoutAttr.frame = CGRect(
     x: 0, y: contentSize.height - CGFloat(indexPath.item + 1) * cellHeight, 
     width: contentSize.width, height: cellHeight) 

    return layoutAttr 
} 

func numberOfItemsInSection(_ section: Int) -> Int? { 
    if let collectionView = self.collectionView, 
     let numSectionItems = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: section) 
    { 
     return numSectionItems 
    } 

    return 0 
} 

override var collectionViewContentSize: CGSize { 
    get { 
     var height: CGFloat = 0.0 
     var bounds = CGRect.zero 

     if let collectionView = self.collectionView { 
      for section in 0 ..< collectionView.numberOfSections { 
       if let numItems = numberOfItemsInSection(section) { 
        height += CGFloat(numItems) * cellHeight 
       } 
      } 

      bounds = collectionView.bounds 
     } 

     return CGSize(width: bounds.width, height: max(height, bounds.height)) 
    } 
} 

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { 
    if let oldBounds = self.collectionView?.bounds, 
     oldBounds.width != newBounds.width || oldBounds.height != newBounds.height 
    { 
     return true 
    } 

    return false 
    } 
} 
+0

你的問題是什麼?你需要實現什麼? –

+0

我有一個UICollectionView自定義單元格。 Swift新手。現在,單元格不可點擊。我希望單元格可點擊並導航到新的ViewController,從單擊的單元格傳遞數據。現在,我甚至無法讓單元格點擊。 – tccpg288

+0

@ tccpg288基於上面的評論,使用'collection View(_:did Select Item At :)'方法可能不是你想要做的。我提供了一個可能適合您需求的答案。 –

回答

2

您必須實現的UICollectionViewDelegate

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    debugPrint("this works") 
    } 

這個方法我希望這幫助你

+0

我添加了代碼來測試並查看我的點擊是否被識別爲無結果。對此很感興趣,非常感謝你的幫助。 – tccpg288

+0

@ tccpg288在實施此方法時,您提供了完成您的需求的邏輯。當單元格在{{}之間輕擊時,添加您想要執行的代碼。 –

+0

所以我的代碼看起來正確?請記住,我正在使用Firebase數據源,可能會有所不同。我只是想打印到日誌點擊來測試它是否正常工作,而不是打印到日誌中。我聽說iOS模擬器在打印日誌時遇到問題。 – tccpg288

1

這個函數用來選擇你的收藏視圖的行

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

} 

然後你使用函數調用。無論您需要什麼,只需定義內部函數即可。並且在所選行(在didSelectItemAt indexPath內)時輕鬆地調用該函數

1

請確保您爲UILabel和UIImageView都啓用了UserIntraction,因爲它們的默認值均爲false。

self.dataSource = self.collectionView?.bind(to: self.ref.child("Polls")) { collectionView, indexPath, snap in 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCell 
    /* populate cell */ 
    cell.pollQuestion.userInteractionEnabled = true; 
    cell.imageView.userInteractionEnabled = true; 
    cell.pollQuestion.text = snap.childSnapshot(forPath: "question").value as! String? 
    let urlPollImage = snap.childSnapshot(forPath: "image_URL").value as! String? 
    cell.imageView.sd_setImage(with: URL(string: urlPollImage!), placeholderImage: UIImage(named: "Fan_Polls_Logo.png")) 
    //Comment 
    return cell 
} 

然後,你需要使用UICollectionViewDelegate方法:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    // handle tap events 
    } 
+0

這些去在viewDidLoad()方法嗎? – tccpg288

+1

是的,你需要在viewDidLoad()方法中添加。 –

相關問題