首页 文章

UITolViewView内部的UIColViewView

提问于
浏览
0

我试图在TableViewCell中设置一个CollectionView . 我已经阅读了一堆完整的堆栈问题,tuts和视频,到目前为止,我有一些似乎正确的方法,但我的集合视图仍未加载到我的表格视图单元格中 .

码:

import UIKit

class TheaterCell: UITableViewCell {

@IBOutlet var theaterName: UILabel!

@IBOutlet var theaterLocation: UILabel!

@IBOutlet var showtimesCollection: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()

    showtimesCollection.delegate = self
    showtimesCollection.dataSource = self
    showtimesCollection.reloadData()

}

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

    // Configure the view for the selected state
}

}

extension TheaterCell: UICollectionViewDataSource, UICollectionViewDelegate {

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

    return 10

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = showtimesCollection.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! TimeCell

    cell.time.text = "1:00"

    return cell

}

}

Tableview从ViewController加载并显示其单元格和元素,但集合视图未在单元格中加载 .

2 回答

  • 0

    我做什么,我使用storyboard并通过拖入类来设置Storyboard中的委托和数据源 .

    a)将TableView的委托和数据源设置为ViewController

    b)将CollectionView的委托和数据源设置为TableViewCell(TheaterCell)

    ViewController代码:

    class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    }
    
    extension ViewController:UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return  1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let theaterCell:TheaterCell = tableView.dequeueReusableCell(withIdentifier: "TheaterCell", for: indexPath) as! TheaterCell
        theaterCell.reloadCollectionView()
        return theaterCell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
    }
    

    TheaterCell代码:

    class TheaterCell: UITableViewCell {
    @IBOutlet var showtimesCollection: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    func reloadCollectionView() -> Void {
        self.showtimesCollection.reloadData()
    }
    }
    
    extension TheaterCell: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = showtimesCollection.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! TimeCell
        cell.time.text = "1:00"
        return cell
    }
    }
    

    TimeCell代码:

    class TimeCell: UICollectionViewCell {
        @IBOutlet var time: UILabel!
    }
    

    这是输出:

    NOTE: IF YOU ARE NOT USING STORYBOARDS AND YOU MAKE COLLECTIONVIEW OR TABLE FROM CODE ONLY, THEN YOU HAVE REGISTER YOURS CELL AS: A) TheaterCell must be registered into ViewController class B) TimeCell must be registered into TheaterCell class

  • 2

    这对我有用,我认为问题来自你注册你的细胞的方式

    class YourVC: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        registerCell()
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }
    func registerCell() {
        collectionView.register(TimeCell.self, forCellWithReuseIdentifier: "cell")
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TimeCell
        cell.time.text = "1:00"
        return cell
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
    }
    

相关问题