首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift:如何以编程方式在CollectionView中显示TableViewCell

Swift:如何以编程方式在CollectionView中显示TableViewCell
EN

Stack Overflow用户
提问于 2020-10-01 15:10:58
回答 1查看 899关注 0票数 0

这段代码在一个collectionView中显示一个tableViewCell,其中两个都是在StoryBoard中添加的:

代码语言:javascript
运行
复制
class TableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
       collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

extension TableViewCell:  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 139, height: 64)
    }

在我的应用程序中,我是以编程方式创建CollectionView和TableView的。

以编程方式创建collectionView:

代码语言:javascript
运行
复制
class collectionViews {
     static func collectionViewOne() -> UICollectionView {
        let flowLayout = CarouselFlowLayout()
        let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)
        return collectionViewOne
    }
}

在tableViewCell中实例化collectionView:

代码语言:javascript
运行
复制
class TableViewCell2: UITableViewCell {
    var moviesItems: [movieItem] = []
    let cellIdentifier = "movieCardCell"
    let collectionViewOne = collectionViews.collectionViewOne()

    private func setupCollectionView(){

        collectionViewOne.delegate = self
        collectionViewOne.dataSource = self
        collectionViewOne.backgroundColor = UIColor (hex: "444A64")
         collectionViewOne.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
                 let nib = UINib(nibName: "movieCardCell", bundle: nil)
        collectionViewOne.register(nib, forCellWithReuseIdentifier: cellIdentifier)

    }
}

// These functions never get called
 extension TableViewCell:  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     print("INSIDE TableViewCell2.collectionView 1")
     return 1
 }
 
 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
     print("INSIDE TableViewCell2.collectionView 2")
     return cell
 }
 
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     // For some reason he chose the measures of collectionViewCell and substracted 2
     print("INSIDE TableViewCell2.collectionView 3")
     return CGSize(width: 139, height: 64)
 }
 }

注意:从来不调用委托函数。因此,collectionView从未添加到tableViewCell?中。

以编程方式创建TableView:

代码语言:javascript
运行
复制
class sectionTableCell: TableViewCell2 {
}
class MoviesViewController4: UIViewController {
    let tableView = UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor (hex: "444A64")
        tableView.delegate = self
        tableView.dataSource = self
         tableView.register(sectionTableCell.self, forCellReuseIdentifier: "tableViewCell")
        displayTableView2()
    }

    func displayTableView2() {
        self.view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    }
}


extension MoviesViewController4: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell =
            tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? sectionTableCell
          else {
                fatalError("Unable to create explore table view cell")}
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
}

所以问题是:如何将我以编程方式创建的collectionView添加到我也以编程方式创建的tableViewCell?

EN

回答 1

Stack Overflow用户

发布于 2020-10-01 16:45:01

据我所见,代码中的任何地方都不会调用setupCollectionView()方法。您应该在init的UITableViewCell方法中这样做。将此代码添加到单元格类中:

代码语言:javascript
运行
复制
override init(frame: CGRect) {
    super.init(frame: frame)
    setupCollectionView()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    setupCollectionView()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64158246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档