2017-07-14 148 views
0

我有一個tableview單元格,其中我添加了collectionview單元格(用於水平滾動)。將目標添加到tableview單元格內的collectionview單元格的按鈕

現在我想推到任何水平collectionview單元格的按鈕上的其他導航控制器。怎麼做 ? Ø

代碼:

ViewController.swift:

class ViewController: UIViewController { 
    var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"] 
} 

extension ViewController : UITableViewDelegate { } 

extension ViewController : UITableViewDataSource { 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return categories[section] 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return categories.count 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow 
     return cell 
    } 

} 

CategoryRow.swift

class CategoryRow : UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

extension CategoryRow : UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 12 
    } 

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

cell.button.addTarget(self, action:#selector(ViewController.goToLookAtPage(_:)), for: .touchUpInside) 
     return cell 
    } 

} 

extension CategoryRow : UICollectionViewDelegateFlowLayout { 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let itemsPerRow:CGFloat = 4 
     let hardCodedPadding:CGFloat = 5 
     let itemWidth = (collectionView.bounds.width/itemsPerRow) - hardCodedPadding 
     let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) 
     return CGSize(width: itemWidth, height: itemHeight) 
    } 

} 

我在哪裏可以申報goToLookAtPage功能?

VideoCell.swift

class VideoCell : UICollectionViewCell { 

    @IBOutlet weak var button: UIButton! 
} 
+1

按我的理解,它應該是在'CategoryRow'類,那麼你需要調用委託或關閉通知你的VC –

+0

我明白,應該也在goToLookAtPage。在那裏你可以調用一個segue,實例化一個新的視圖控制器或發佈通知從其他地方調用你的視圖控制器 – GIJOW

+0

我建議從VideoCell調用委託給CategoryRow,然後讓viewcontroller實現CategoryRow委託,你可以在那裏導航。 –

回答

2

你需要delclare它 上CategoryRow類也聲明全局關閉

var buttonTapped:((CategoryRow?) -> Void)? = nil 

現在我們closure調用

實現goToLookAtPage如下

func goToLookAtPage(_ sender:UIButton) { 

    if let btnAction = self.buttonTapped { 
     btnAction(self) 
     } 
} 

現在,在您ViewController加入cellForRowAtIndexPath

cell.buttonTapped = {(cell) -> Void in 
      //You Got your response , do push in main Thread 
} 

以下希望它可以幫助你

+0

謝謝:)它的工作。你能告訴我,如果點擊一下,我該如何更改該按鈕的標題? –

+1

你可以在'goToLookAtPage'上更改它,你有發送者 –

+0

噢,謝謝喲這麼多:) –

0

在您的按鈕處理函數獲取按鈕的位置,發件人是你的按鈕。

let position = sender.convert(CGPoint.zero, to: self.collectionView) 

獲取給定位置的索引路徑。

let indexPath = self.collectionView?.indexPathForItem(at: position) 

從索引路徑的數據源中獲取模型。

let model = self.data[indexPath.row] 

既然你有你的數據模型,把它傳遞到你的目的地,並推動你的視圖控制器或任何其他。

+1

這不是問題的答案,這是'我在哪裏聲明goToLookAtPage函數?' –

+0

@MikeAlter我認爲你是對的。警告後,我意識到集合視圖單元格在表格視圖單元格內。我甚至不想知道爲什麼會這樣。謝了哥們。 – Desdenova

相關問題