将 XIB 视图重用于集合视图作为其他视图的一部分?

Reusing XIB views for collectionviews as a partial for other views?

我想为 collection 视图和其他视图重复使用一个 XIB 文件。

例如,我有一张卡片需要显示为 collection 视图单元格,但我也想将相同的 xib/view 用于独立页面。

即;

我想对 collection 视图和普通视图使用相同的卡片图像,几乎就像是部分视图一样;因此可以在任何需要的地方使用它。

我这样注册手机;

self.myCollectionView.register(UINib(nibName: "MyCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "myCellIdentifier")

那就用吧;

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellIdentifier", for: indexPath) as! MyCollectionViewCell

...
}

我尝试使用我创建的 UIView 转换 dequeueReusableCell 代码,但它不喜欢以这种方式使用视图。

单元格和我的 XIB 视图文件都是独立的 swift 文件,因此它不在情节提要中。

如何将一个 uiview 用于 collection 个视图,然后在其他页面上重复使用相同的视图?

非常感谢

总结

我想在应用程序的多个地方使用一个 UIView 和 XIB 文件

目前,我有一个 collection 视图,它使用 UICollectionViewCell 和关联的 XIB 文件

我想在应用程序的许多地方使用同一个 XIB 文件。

如何在我的 collection 视图和应用程序中我需要它的地方重用同一个 UIView?

谢谢

(a) I have a swift file which I created; it a custom subclassed UICollectionViewCell. It has a XIB file.

使用集合视图注册笔尖。在 cellForItemAt 中,当您需要一个单元格时,将其出列。您将从 nib 收到一份集合视图单元格的副本。根据需要配置它。 Return它。

(b) I want to reuse the same XIB elsewhere in the app, including all outlets and connections

加载 nib 并拉出第一个(也是唯一一个)顶级视图。您将从 nib 收到一份集合视图单元格的副本。将其作为子视图添加到界面中的视图。

(最简单的方法是通过 UINib 及其 instantiate 方法。这个 returns 一个顶级对象数组。)

是的,通常不会将集合视图单元格用作普通视图的子视图。但我想不出任何理由反对它;毕竟,视图就是视图。

(如果您不想这样做,我可以想到解决方法;例如,将 nib 中的视图设为自定义 UIView 并通过加载 nib 并将该视图放入单元格来配置每个单元格。这会带来一些不便,但它会使笔尖更普遍使用。)


我举了一个简单的例子。这是笔尖,非常简单,因为它只是一个例子:

这是我的应用程序,显示了一个集合视图(在蓝色背景上显示了四个单元格)以及从 nib 中提取并粘贴到界面中的视图:

这是完整的代码:

class MyView : UIView {
    @IBOutlet var iv : UIImageView?
}

class MyCell : UICollectionViewCell {
    var v : MyView?
}

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    @IBOutlet weak var cv: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.cv.register(MyCell.self, forCellWithReuseIdentifier: "cell")
        let arr = UINib(nibName: "View", bundle: nil).instantiate(withOwner: nil, options: nil)
        let v = arr[0] as! UIView
        self.view.addSubview(v)
        v.frame.origin = CGPoint(x:20, y:300)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCell
        if cell.v == nil {
            let arr = UINib(nibName: "View", bundle: nil).instantiate(withOwner: nil, options: nil)
            let v = arr[0] as! MyView
            cell.contentView.addSubview(v)
            cell.v = v
        }
        print(cell.v?.iv) // image view
        return cell
    }

}

注意子类和插座的使用。 cell.v 将我们带到视图,因此 cell.v?.iv 将我们带到图像视图——现在我们可以根据需要执行进一步的配置。因此我们添加了一个间接级别以便从笔尖到达子视图,但这似乎是一个非常小的代价。