【问题标题】:CollectionView 2 sections. How to access the outlet vars from inner class in swiftCollectionView 2 部分。如何快速从内部类访问出口变量
【发布时间】:2018-04-25 00:24:36
【问题描述】:

我的收藏视图中有 2 个部分:图片和相册。 每个部分都有自己的逻辑。通过点击相册 - 一些照片被上传。 通过单击图像我想访问出口 UIView 变量,但我不能因为内部类中的 didSelectItemAt indexPath 方法。

class TwoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

        @IBOutlet var pickedPhotoView: UIView!

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath) as! AlbumCell


           <b>// Here is ok. i can access the pickedPhotoView variable</b>

            view.addSubview(pickedPhotoView)


        }


    class ImagesCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            // but here there is no chance to access pickedPhotoView variable


    }
}     

更新:

 class TwoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, ImagesCellDelegate {

        func doSomething(_ sender: ImagesCell) {
            print("do something")
        }


        var imagesCell: ImagesCell = ImagesCell()

        override func viewDidLoad() {
            super.viewDidLoad()
            imagesCell.delegate = self
       }
}

protocol ImagesCellDelegate {
    func doSomething(_ sender: ImagesCell)
}

class ImagesCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    var delegate: ImagesCellDelegate!

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        delegate?.doSomething(self)

    }
}

doSomething 函数未在 TwoViewController 中执行。 我想我做错了什么:

var imagesCell: ImagesCell = ImagesCell()
imagesCell.delegate = self

有什么想法吗? 谢谢!

解决办法是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     cell.delegate = self
}

【问题讨论】:

  • 为什么单元格冒充集合视图委托?让控制器成为代表...
  • Mohit Athwani,我该怎么做?

标签: ios swift xcode collectionview


【解决方案1】:

我无法发表评论,因为我没有足够的声誉,但您的问题并不清楚。 您有 2 个部分 --> 您可以在 TwoViewController 中确定当前选择部分。

if indexPath.section == 0 {
// album
} else if indexPath.section == 1 {
// photo 
}

或者,如果您想在用户与每个特定单元格交互时触发,您可以使用委托或闭包/阻塞回调。

【讨论】:

    【解决方案2】:

    为什么必须将 ImagesCell 作为内部类? “ 'TwoViewController' 类型的实例成员 'pickedPhotoView' 不能用于嵌套类型 'TwoViewController.ImagesCell' 的实例” 您可以在 TwoViewController 之外制作 ImagesCell,就像 AlbumCell 一样。

    【讨论】:

      【解决方案3】:

      简而言之,iOS 中需要委托模式。


      代码如下。

      class ImagesCell : : UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
      
          var delegate: ImagesCellDelegate!
      }
      
      protocol ImagesCellDelegate {
          func doSomething()
      }
      
      extension TwoViewController: ImagesCellDelegate {
          func doSomething() {
              // pickedPhotoView can be accessed here
          }
      }
      


      更多

      UITableViewCellUICollectionViewCell 不应访问UIViewController 中的成员变量。这些单元格应该专注于他们的 UI 呈现,并且不应该涉及任何关于它的父视图控制器的知识。

      通过这种方式,这些单元格可以很容易地在另一个 UIViewController 中重用。那些 UIViewController 只知道他们什么时候想使用ImagesCell,他们MUST 实现ImageCellDelegate 协议。

      【讨论】:

      • 感谢您的回复!我试过了,但函数 doSomething() 没有在我的主控制器中捕获 - TwoViewContrller
      【解决方案4】:

      解决办法是:

      class TwoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, ImagesCellDelegate {
      
              func doSomething(_ sender: ImagesCell) {
                  print("do something")
              }
      
      
              var imagesCell: ImagesCell = ImagesCell()
      
              override func viewDidLoad() {
                  super.viewDidLoad()
                  imagesCell.delegate = self
             }
      }
      
      protocol ImagesCellDelegate {
          func doSomething(_ sender: ImagesCell)
      }
      
      class ImagesCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
      
          var delegate: ImagesCellDelegate!
      
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      
              delegate?.doSomething(self)
      
          }
      }
      
          func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
               // just added this code in function
               cell.delegate = self
          }
      

      【讨论】:

        猜你喜欢
        • 2011-06-15
        • 2018-03-04
        • 1970-01-01
        • 2012-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多