从CollectionView单元中以编程方式推送ViewController不起作用-Swift

时间:2018-07-23 09:04:27

标签: ios swift uinavigationcontroller uicollectionviewflowlayout

我是一个新的iOS程序。现在,我通过不使用storyboard来创建示例应用程序,我对滚动方向是垂直的主类实现了UICollectionView。我为一个特定的单元格添加了另一个UICollectionView用于水平方向。问题是cell的水平滚动方向没有推到我单击的另一个视图控制器上。

这是我在rootViewController中设置的AppDelegate

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let navigation = UINavigationController(rootViewController: CategoryController())
window?.rootViewController = navigation

这里是CategoryController,我实现了tableView

tableView.register(TableCell.self, forCellReuseIdentifier: cellId)

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

    return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 120
}

对于tableView,当我想导航到特定控制器时,单击特定单元格可以正常工作

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.item)
    let view = DetailEachCellController()
    navigationController?.pushViewController(view, animated: true)
}

问题在于,在DetailEachCellController中,我使用UICollectionView列出了一些数据,当我单击特定的单元格时,并没有导航到新的控制器

这里DetailEachCellControllerUIViewController

var collectionDetailView: UICollectionView!
collectionDetailView.register(DetailContactCell.self, forCellWithReuseIdentifier: cellIdContact)
collectionDetailView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdContent)

func setupCollectionView() {
    collectionDetailView.topAnchor.constraint(equalTo: detailContent.topAnchor).isActive = true
    collectionDetailView.widthAnchor.constraint(equalTo: detailContent.widthAnchor).isActive = true
    collectionDetailView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    collectionDetailView.backgroundColor = .white 
}

func callAction() {
    print("pushing view")
    let view = UIViewController()
    self.navigationController?.pushViewController(view, animated: true)
}

这里是UICollectionViewFlowLayout的代表

extension DetailEachCellController : UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.item == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContact, for: indexPath)

            return cell
        }
        else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContent, for: indexPath)
            cell.backgroundColor = .blue
            return cell
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.item == 1 {
          return CGSize(width: view.frame.width, height: 250)
        }
        return CGSize(width: view.frame.width, height: 120)
    }
}

这里是DetailContactCell,我添加了一些按钮以供用户单击,并在下一个视图中添加了更多按钮,但它没有移动,但调用了该函数。

class DetailContactCell: UICollectionViewCell {

var eachCellController: DetailEachCellController?

lazy var callButton: UIButton = {

    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.layer.shadowOpacity = 0.25
    button.backgroundColor = .white
    button.setTitle("CALL", for: .normal)
    button.layer.cornerRadius = 10
    button.layer.shadowOffset = CGSize(width: 0, height: 10)
    button.layer.shadowRadius = 10
    return button
}()
lazy var bookButton: UIButton = {

    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.layer.shadowOpacity = 0.25
    button.backgroundColor = .white
    button.layer.cornerRadius = 10
    button.layer.shadowOffset = CGSize(width: 0, height: 10)
    button.layer.shadowRadius = 10
    return button
}()
override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(callButton)
    addSubview(bookButton)
    callButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    callButton.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 15).isActive = true
    callButton.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/2.5).isActive = true
    callButton.heightAnchor.constraint(equalToConstant: 60).isActive = true

    bookButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    bookButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15).isActive = true
    bookButton.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/2.5).isActive = true
    bookButton.heightAnchor.constraint(equalToConstant: 60).isActive = true

    self.actionButton()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func actionButton() {
    callButton.addTarget(self, action: #selector(callBtn), for: .touchUpInside)
}

@objc func callBtn() {
    print("calling")
    eachCellController = DetailEachCellController()
    eachCellController?.callAction()
}

}

4 个答案:

答案 0 :(得分:3)

因为这个

// eachCellController = DetailEachCellController()//对其进行注释

是另一个VC而不是一个

eachCellController?.callAction()

您需要使用cellForItemAt

发送自我
cell.eachCellController = self

//

@objc func callBtn() {
   print("calling")
   eachCellController?.callAction()
}

答案 1 :(得分:2)

您不能从UICollectionViewCell类内部将新的视图控制器推入导航控制器堆栈。

这样做的方法是(通过委托方法或回调处理程序)告诉DetailEachCellController单击了按钮,然后 ONLY FROM DetailEachCellController推视图控制器。

这里是similar thread

答案 2 :(得分:2)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == 0 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContact, for: indexPath) as! DetailContactCell
        // Here is line add to `UICollectionViewFlowLayout`
        cell.eachCellController = self

        return cell
    }else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContent, for: indexPath)
        cell.backgroundColor = .blue
        return cell
    }

}

答案 3 :(得分:1)

UIViewController的强对象传递到单元格不是方便的方法。您可能会创建保留周期,并且视图控制器将不会像内存一样被释放,因此将来可能会导致内存问题

要解决此问题,您可能有以下选择

  1. 使用代理人
  2. 使用闭包
  3. 使用addTarget在视图控制器本身中执行按钮动作

由于闭包选项很容易,我在此举一个示例来实现它。

在集合视图单元格中,添加以下属性

var callBtnTapped : (() -> ())?

现在进入。

@objc func callBtn() {
    print("calling") 
     // Check that target has implemented closure ? 
     if let clouser = self.callBtnTapped {
            clouser(screenShotID)
      }
}

在您的视图控制器cellForItem中,将主体添加到闭包

     cell.callBtnTapped = {[weak self]  in
        guard let strongSelf = self   else  {return}
        strongSelf. callAction()
        // It will be called when button tapped 
    }

希望对您有帮助

相关问题