navigate on click of collectionview cell inside tableview

后端 未结 4 922
悲&欢浪女
悲&欢浪女 2020-12-18 15:49

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

Now i want to push to other navigation controller on pressing any

相关标签:
4条回答
  • 2020-12-18 16:15

    I think its better to use Notification in this case.

    post a notification in didSelectItem of collection view

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

    and add an observer in viewController viewDidLoad as follows

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

    in the new pushToNew function, perform your segue

    func pushToNew(notification: Notification) {
        // perform your segue here. Navigate to next view controller
    }
    
    0 讨论(0)
  • 2020-12-18 16:15

    First create protocol for delegation from CategoryRow.swift like below code

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

    Now create delegate object on VideoCell.swift like below

    weak var delegate:CollectionViewSelectionDelegate? 
    

    Change ViewController.swift code before return cell

    cell?.delegate = self
    

    Override method of delegate in ViewController.swift and call similar method from VideoCell.swift from UICollectionView Delegate method.

    0 讨论(0)
  • 2020-12-18 16:17

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

    Add the delegate function inside ViewController

    func cellTapped(){
    //code for navigation
    }
    
    0 讨论(0)
  • 2020-12-18 16:22

    Make a protocol

    protocol collectionViewCellClicked{
        func cellClicked()
    }
    
    • Implement this protocol in main View Controller Your View Controller look like this

      class ViewController: UITableViewController, collectionViewCellClicked{ func cellClicked(){ // Your Code  }}
      
    • Your cellForRowAt delegate look like this

      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?

      and in your didSelectItemAt delegate

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

    0 讨论(0)
提交回复
热议问题