Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在collectionViewCell和tableViewCell中使用相同的视图_Ios_Swift_Uitableview_Uicollectionview - Fatal编程技术网

Ios 在collectionViewCell和tableViewCell中使用相同的视图

Ios 在collectionViewCell和tableViewCell中使用相同的视图,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我使用自定义collectionViewCell创建了一个nib文件,并附加到一个viewController class CustomCollectionView: UICollectionViewCell{} class CustomTableView: UITableViewCell{} 现在我必须使用tableView中的精确单元格。我创建了一个新的nib文件和viewController class CustomCollectionView: UICollectionViewCel

我使用自定义collectionViewCell创建了一个nib文件,并附加到一个viewController

class CustomCollectionView: UICollectionViewCell{}
class CustomTableView: UITableViewCell{}
现在我必须使用tableView中的精确单元格。我创建了一个新的nib文件和viewController

class CustomCollectionView: UICollectionViewCell{}
class CustomTableView: UITableViewCell{}

我在上面复制了
CustomCollectionView
的孔代码。一切正常,但我认为将
CustomCollectionView
的确切代码复制到
CustomTableView
中,并使用完全相同的nib文件,但使用
tableViewCell
而不是
collectionViewCell
,是没有意义的。有没有办法优化我的工作

如果要使用相同的视图,则最好使用类似类型的视图,即在两个位置都使用collectionView,这样您就可以在两个
视图控制器中使用
CustomCollectionViewCell
UICollectionView
是高度可定制的,因此您也可以在
UICollectionView
中使用
UITableView
执行任何您想执行的操作。

正如您在suhit回答中的评论中所说,您可以通过在
CollectionViewCell
TableViewCell
子类中使用公共视图来完成此操作。您不需要ViewController,因为它会增加额外的开销。一个简单的UIView就足够了。一些代码来说明我的意思:

class CustomTableViewCell: UITableViewCell {

    var customView: CustomView!

    func awakeFromNib() {
        super.awakeFromNib()
        customView = CustomView()
        customView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(customView)
        customView.fillSuperview()
    }
}

class CustomCollectionViewCell: UICollectionViewCell {

    var customView: CustomView!

    func awakeFromNib() {
        super.awakeFromNib()
        customView = CustomView()
        customView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(customView)
        customView.fillSuperview()
    }
}


extension UIView {

    func fillSuperview() {
        guard let superview = superview else {
            return print("no superview")
        }
        topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
        bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
        leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
        rightAnchor.constraint(equalTo: contentVisuperviewew.rightAnchor).isActive = true

    }

}
CustomView
类的示例实现:

class CustomView: UIView {

    func initialize() {
        //...
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }

}

如果您希望在xib中创建自定义视图,也可以,但这有点棘手。这超出了问题的范围,但我只想留下一个链接,以备您需要

将tableView更改为collectionView不是我的选项。将视图创建为UIViewController,然后将其加载到单元格中,这可能是一个更好的主意吗?使用集合视图单元格将删除大量需要编写的重复代码,因为您使用xib创建自定义单元格,所以替换代码很简单。对我来说,这种方法似乎比建立一个共同的观点要好。我不确定我是否理解你。您的意思是在tableView中使用collectionViewCell?这有用吗?还是说使用两个collectionView而不是tableView?正如我所说,我正在处理一个现有项目,第二个选项不可能“在tableView中使用collectionViewCell”-否,“使用两个collectionView而不是tableView”-是的。如果功能相同,那么用collectionView替换tableview没有任何问题,选择权在您。干杯。功能完全不同,这就是我使用表视图的原因。但只有一个细胞是相同的。。。