我有一個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!
}
按我的理解,它應該是在'CategoryRow'類,那麼你需要調用委託或關閉通知你的VC –
我明白,應該也在goToLookAtPage。在那裏你可以調用一個segue,實例化一個新的視圖控制器或發佈通知從其他地方調用你的視圖控制器 – GIJOW
我建議從VideoCell調用委託給CategoryRow,然後讓viewcontroller實現CategoryRow委託,你可以在那裏導航。 –