Ios CollectionView 2部分。如何在swift中从内部类访问出口变量

Ios CollectionView 2部分。如何在swift中从内部类访问出口变量,ios,swift,xcode,collectionview,Ios,Swift,Xcode,Collectionview,我的收藏视图中有两个部分:图像和相册。 每个部分都有自己的逻辑。点击相册-上传一些照片。 通过单击图像,我想访问outlet UIView变量,但由于内部类中的didSelectItemAt indexPath方法,所以无法访问 class TwoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

我的收藏视图中有两个部分:图像和相册。 每个部分都有自己的逻辑。点击相册-上传一些照片。 通过单击图像,我想访问outlet 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


    }
}     
doSomething func未在TwoViewController中执行。 我想我在以下方面做错了:

var imagesCell: ImagesCell = ImagesCell()
imagesCell.delegate = self
有什么想法吗? 塔克斯

解决办法是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     cell.delegate = self
}
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
    }

我不能发表评论,因为我没有足够的声誉,但你的问题不清楚。 您有两个区段-->可以在TwoViewController中确定当前选择区段

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

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

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

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


代码如下。

更多
UITableViewCell
UICollectionViewCell
不应访问
UIViewController
中的成员变量。这些单元应该专注于它们的UI表示,并且不应该涉及关于其父视图控制器的任何知识

通过这种方式,这些单元可以轻松地在另一个UIViewController中重用。那些UIViewController只知道何时要使用
ImagesCell
,他们
必须实施
ImageCellDelegate
协议。

解决方案是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     cell.delegate = self
}
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
    }

为什么该单元格冒充集合视图委托?让控制员成为代表…Mohit Athwani,我该怎么做?谢谢你的回复!我试过了,但是doSomething()函数没有被我的主控制器TwoViewContrller捕获