在tableview内的collectionview单元格的单击上导航 [英] navigate on click of collectionview cell inside tableview

查看:74
本文介绍了在tableview内的collectionview单元格的单击上导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableview单元格,在其中添加了collectionview单元格(用于水平滚动).

I have a tableview cell inside which i have added collectionview cell ( for horizontal scrolling).

现在,我想在按下水平collectionview的任何单元格时推送到其他导航控制器.怎么做 ?或如何定义细胞压榨的委托方法.

Now i want to push to other navigation controller on pressing any cell of horizontal collectionview. How to do it ? Or how can i define delegate methods for cell press.

代码:

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
        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)
    }

}

VideoCell.swift

class VideoCell : UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}

推荐答案

在这里,您将在CategoryRow类上的委托方法didSelectItemAtIndexPath上单击单元格,然后可以激发一个委托以在 ViewController.swift :

Here you will get the cell click at the delegate method didSelectItemAtIndexPath on CategoryRow class and from there you can fire a delegate to get call inside ViewController ViewController.swift :

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

VideoCell.swift :

    protocol CategoryRowDelegate:class {
    func cellTapped()
    }

CategoryRow.swift :

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

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if delegate!= nil {
    delegate?.cellTapped()
    }
    }

ViewController

func cellTapped(){
//code for navigation
}

这篇关于在tableview内的collectionview单元格的单击上导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆