简体   繁体   English

在tableview内的collectionview单元格的单击上导航

[英]navigate on click of collectionview cell inside tableview

I have a tableview cell inside which i have added collectionview cell ( for horizontal scrolling). 我有一个tableview单元格,其中添加了collectionview单元格(用于水平滚动)。

Now i want to push to other navigation controller on pressing any cell of horizontal collectionview. 现在我想在按下水平collectionview的任何单元格时推送到其他导航控制器。 How to do it ? 怎么做 ? Or how can i define delegate methods for cell press. 或如何定义单元格印刷的委托方法。

Code : 代码:

ViewController.swift : 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 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 VideoCell.swift

class VideoCell : UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}

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 : 在这里,您将在CategoryRow类的委托方法didSelectItemAtIndexPath处单击单元格,然后可以从该位置激发一个委托以在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 : VideoCell.swift

    protocol CategoryRowDelegate:class {
    func cellTapped()
    }

CategoryRow.swift : 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()
    }
    }

Add the delegate function inside ViewController ViewController添加委托函数

func cellTapped(){
//code for navigation
}

I think its better to use Notification in this case. 我认为在这种情况下最好使用Notification。

post a notification in didSelectItem of collection view 在集合视图的didSelectItem中发布通知

NSNotificationCenter.defaultCenter().postNotificationName(notificationIdentifier, object: nil)

and add an observer in viewController viewDidLoad as follows 并在viewController viewDidLoad添加一个观察者,如下所示

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pushToNew(_:)), name: notificationIdentifier, object: nil)

in the new pushToNew function, perform your segue 在新的pushToNew函数中,执行搜索

func pushToNew(notification: Notification) {
    // perform your segue here. Navigate to next view controller
}

First create protocol for delegation from CategoryRow.swift like below code 首先从CategoryRow.swift创建委派协议,如下代码

protocol CollectionViewSelectionDelegate: class {
    func didSelectedCollectionViewItem(selectedObject: AnyObject)
}

Now create delegate object on VideoCell.swift like below 现在在VideoCell.swift上创建委托对象,如下所示

weak var delegate:CollectionViewSelectionDelegate? 

Change ViewController.swift code before return cell 在返回单元格之前更改ViewController.swift代码

cell?.delegate = self

Override method of delegate in ViewController.swift and call similar method from VideoCell.swift from UICollectionView Delegate method. 重写ViewController.swift中的委托方法,并从UICollectionView Delegate方法中调用VideoCell.swift中的类似方法。

Make a protocol 制定协议

protocol collectionViewCellClicked{
    func cellClicked()
}
  • Implement this protocol in main View Controller Your View Controller look like this 在主View Controller中实现此协议。View Controller如下所示

     class ViewController: UITableViewController, collectionViewCellClicked{ func cellClicked(){ // Your Code }} 
  • Your cellForRowAt delegate look like this 您的cellForRowAt委托看起来像这样

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: <#T##String#>, for: <#T##IndexPath#>) cell.delegate = self return cell }

  • In your Table View Cell Make a variable of type collectionViewCellClicked var delegate: collectionViewCellClicked? 在表视图单元格中,创建一个类型为collectionViewCellClicked的变量变量 var delegate: collectionViewCellClicked?

    and in your didSelectItemAt delegate 并在您的didSelectItemAt委托中

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { delegate.cellClicked() }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从collectionview中的Tableview单元导航到查看控制器 - Navigate to view controller from Tableview cell inside collectionview tableview单元格内的Collectionview不会增加collectionView的高度 - Collectionview inside tableview cell not increasing height of collectionView collectionview在tableview单元格内重复 - collectionview are repeating inside a tableview cell 如何使用委托从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController? - How to Navigate to another ViewController from Collectionview cell which is inside Tableview cell using delegate? 如何从位于 tableView 单元格内的 collectionView 的 didSelectItemAt 导航到 viewController? - How can I Navigate to a viewController from a collectionView's didSelectItemAt which is inside a tableView cell? 如何在tableview单元格内的collectionview单元格的按钮单击时获取索引 - How to get index on button click of collectionview cell which is inside tableview cell 在 tableview 单元格内为 collectionview 加载更多功能 - Load more functionality for collectionview inside tableview cell tableview单元格索引路径内的Collectionview问题 - Collectionview inside tableview cell indexpath issue 带有1个数据源的tableview单元格内的Collectionview - Collectionview inside tableview cell with 1 data source 单元重用tableview内collectionview中的另一个图像 - cell reuse another image in collectionview inside tableview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM