如何将新的UICollectionViewController推入单元格的单元格内的navigationController

时间:2019-01-05 23:59:09

标签: ios swift uinavigationcontroller uicollectionviewcell uicollectionviewdelegate

我已经以编程方式构建了一个UICollectionViewController,它返回了4个单元格。 HomeCell,TrendingCell,SubscriptionCell和AccountCell。 所有4个单元格应该都不同,您可以水平滚动它们。 <--->。

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout{



    override func viewDidLoad() {
        super.viewDidLoad()
        
         collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
        collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId)
        collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId)
        collectionView?.register(AccountCell.self, forCellWithReuseIdentifier: accountCellId)
        
        }
        
        
        
 }

让我们以HomeController的第一个单元为HomeCell来说明我的问题。 Homecell具有三个自定义单元,分别称为VideoCell,CategoryCell和UserSearchCell。

class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    
    let cellId = "cellId"
    let searchId = "sarchId"
    let scrollId = "scrollId"
    
    
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()
    
    
      override func setupViews() {
        super.setupViews()
  .....

// register  three different cells within HomeCell
        collectionView.register(VideoCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.register(CategoryCell.self, forCellWithReuseIdentifier: scrollId)
        collectionView.register(UserSearchCell.self, forCellWithReuseIdentifier: searchId) //
        
        
    }
    
    
    
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
    

在HomeCell中,我将UserSearchCell注册为第三个单元。

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       
     
        if indexPath.item == 0 {
            
              cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoId", for: indexPath)
        } else if indexPath.item == 1{
            
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryId, for: indexPath)
            
        }else {
            
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchId, for: indexPath)
           
        }
        
        
        return cell
        
    }

如果我单击该元素,我的目标是将新的ViewController推送到navigationController。但是我没有访问权限,也不知道如何在嵌套结构中更改视图。我尝试了HomeCell类中的didSelectItem方法,并在单击第三个单元格时能够在控制台上打印一些内容,但无法更改视图。

class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 2 {
            
        print(123)
        
        }
        
        ....
        
   }

请帮助。有没有一种方法可以在HomeCell的didSelectItem方法中更改View?

1 个答案:

答案 0 :(得分:3)

您需要编写一个协议以引用回HomeController。

protocol HomeCellProtocol {
    func pushNavigation(_ vc: UIViewController)
}

使用以下命令将写委托属性添加到HomeCell类中

class HomeCell: ..... {
    var delegate: HomeCellProtocol?
}

并使HomeController通过以下方式确认HomeCellProtocol

extention HomeController: HomeCellProtocol {
    func pushNavigation(_ vc: UIViewController) {
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

当您设置HomeCell时,您需要在HomeController中设置委托

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    cell = collectionView.dequeueReusableCell(withReuseIdentifier: homeCellId, for: indexPath) as HomeCell;
    cell.delegate = self // Set the delegate
    return cell
}

最后,您可以使用以下命令在HomeCell中调用推送功能

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.item == 2 {
        let vc = UIViewController();
        self.delegate?.pushNavigation(vc);
    }
}