ios - 如何将新的 UICollectionViewController 推送到单元格单元格内的导航 Controller

标签 ios swift uinavigationcontroller uicollectionviewcell uicollectionviewdelegate

我以编程方式构建了一个返回 4 个单元格的 UICollectionViewController。 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。但我无权访问,也不知道如何更改该嵌套结构中的 View 。我尝试了 HomeCell 类中的 didSelectItem 方法,在单击第三个单元格时能够在控制台打印一些内容,但无法更改 View 。

class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


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

请帮忙。有没有办法在 HomeCell 的 didSelectItem 方法中更改 View ??

最佳答案

您需要编写一个协议(protocol)来引用您的 HomeController。

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

将写入委托(delegate)属性添加到 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);
    }
}

关于ios - 如何将新的 UICollectionViewController 推送到单元格单元格内的导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54057441/

相关文章:

ios - 无法让状态恢复与程序化导航 Controller 一起使用

ios - tableView 数据源的字典

ios - 如何以编程方式移动 UIButton?

iOS 6 : supportedInterfaceOrientations is called but shouldAutorotate is not called

objective-c - 在选项卡栏和导航栏内弹出 View 时遇到问题

ios - 如何使用 StoryBoard 在 iPad 上的 Popover 中显示 UIDatePicker?

ios - 在 iOS 中重新加载 tableview 部分时出现残像

ios - 如何让SWRevealViewController slide "over"的main view controller 不是 "push"呢?

swift - 我应该将字符串设置为 Nil 还是 Empty?

ios - UITableView 顶部的 UISegementController