简体   繁体   English

如何在 UITableView 中使用 UICollectionViewCell?

[英]How to use a UICollectionViewCell in a UITableView?

I have a cell originally created as a UICollectionViewCell but I want to be able to reuse it in a UITableView .我有一个最初创建为UICollectionViewCell的单元格,但我希望能够在UITableView中重用它。 Would I simply have to reimplement it in the form of a UITableView or is there any alternative?我是否只需以 UITableView 的形式重新实现它,还是有其他选择?

The UITableView needs to present the exact same cell as well, but it can't because of the lack of cross-compatibility. UITableView需要呈现完全相同的单元格,但由于缺乏交叉兼容性而不能。 My original cell is declared as such and uses a NIB file for the UI layout:我的原始单元格是这样声明的,并为 UI 布局使用 NIB 文件:

class originalCollectionViewCell: UICollectionViewCell {
    //parent view that nests several other views
    @IBOutlet weak var viewBackground: UIView! 

    override func awakeFromNib() {
        ...
    }
}

If I'm understanding the question correctly, I'd recommend creating a subclass of UIView and move the subviews that are currently in your collection view cell into this subclass.如果我正确理解了这个问题,我建议创建一个UIView的子类,并将当前在您的集合视图单元格中的子视图移动到这个子类中。 Then use this subview as the contentView of both your collection view cell and table view cell.然后将此子视图用作集合视图单元格和表格视图单元格的 contentView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM