【问题标题】:Collection View cell in Table View Controller (like Tinder Message tab)表视图控制器中的集合视图单元格(如 Tinder 消息选项卡)
【发布时间】:2020-04-02 03:15:12
【问题描述】:

这是我第一次使用 Stack Overflow :) 希望我能找到答案并提供我的知识。

我目前正在制作一个像 Tinder 或 instagram 风格的聊天应用程序来学习 Swift。

正如您在 Tinder 上看到的,您可以滑动水平匹配的用户

https://i.stack.imgur.com/dFUcM.jpg

我想构建几乎相同的设计。

在我的 main.storyboard 上,就像

TableViewController

   -collectionView

        -collectionViewCell
        -collectionViewFlowLayout

   -tableViewCell

        -contentView

像这样。

collectionView 单元格用于匹配的用户, 和 tableviewCells 用于打开的聊天室。

我已经完成了聊天室的制作。

但集合视图部分永远不会出现。 我尝试为此制作 Extension.swift 文件并输入

 extension TableViewController {


   class RequestCVCell: UICollectionViewCell {

    @IBOutlet weak var MatchedAvatar: UIImageView!
    @IBOutlet weak var MatchedName: UILabel!

   override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
   MatchedAvatar.layer.cornerRadius = 30
   MatchedAvatar.clipsToBounds = true

    }


    let horizontalView = UIView(backgroundColor: .blue)

          }
 }

我想构建一个类似的故事板

ViewController

   -collectionView

        -collectionViewCell
        -collectionViewFlowLayout

   -tableView

        -contentView

我真的不知道为什么它不起作用。 :( 有什么办法可以像火种一样制作吗?

感谢您的阅读,抱歉我的英语缺乏解释。

【问题讨论】:

    标签: ios swift tableview collectionview tinder


    【解决方案1】:

    我在 github 上为你创建了一个示例项目 https://github.com/manojaher88/CollectionViewInTableView

    下面是可能对你有帮助的代码

    // MARK:- CellType
    enum CellType: Int, CaseIterable {
        case newMatches = 0
        case messages
    }
    
    // MARK:- NewMatch
    struct NewMatch {
        let name: String
        let imageName: String
    }
    
    // MARK:- ViewController
    class ViewController: UIViewController {
    
       enum Constants {
        static let nibName = "CollectionTableViewCell"
        static let reuseIdentifier = "CollectionTableViewCell"
    }
    
    @IBOutlet weak var tableView: UITableView!
    
    private var newMatches: [NewMatch] = []
    private var newMessages: [NewMatch] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
        createDataSource()
    }
    
    private func setupTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        let nibName = UINib(nibName: Constants.nibName, bundle: nil)
        tableView.register(nibName, forCellReuseIdentifier: Constants.reuseIdentifier)
    }
    
    private func createDataSource() {
        for i in 0...3 {
            let newMatch = NewMatch(name: "Matches \(i)", imageName: "image_1")
            newMatches.append(newMatch)
        }
    
        for i in 0...3 {
            let newMatch = NewMatch(name: "Messages \(i)", imageName: "image_1")
            newMessages.append(newMatch)
        }
    }
    }
    
    extension ViewController: UITableViewDelegate, UITableViewDataSource  {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return CellType.allCases.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        guard let cell = tableView.dequeueReusableCell(withIdentifier: Constants.reuseIdentifier, for: indexPath) as? CollectionTableViewCell else {
            fatalError()
        }
        var dataSource: [NewMatch] = []
        let cellType = CellType(rawValue: indexPath.section)
        switch cellType {
        case .newMatches:   dataSource = newMatches
        case .messages:     dataSource = newMessages
        case .none: break
        }
        cell.update(newModels: dataSource)
    
        return cell
    }
    }
    
    class CollectionTableViewCell: UITableViewCell {
    
    enum Constants {
        static let nibName = "CollectionViewCell"
        static let reuseIdentifier = "CollectionViewCell"
    }
    
    @IBOutlet private weak var collectionView: UICollectionView!
    private var models: [NewMatch] = []
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        collectionView.delegate = self
        collectionView.dataSource = self
        let collectionViewCell = UINib(nibName: Constants.nibName, bundle: nil)
        collectionView.register(collectionViewCell, forCellWithReuseIdentifier: Constants.reuseIdentifier)
    }
    
    func update(newModels: [NewMatch]) {
        models.removeAll()
        models.append(contentsOf: newModels)
        collectionView.reloadData()
    }
    }
    
    extension CollectionTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constants.reuseIdentifier, for: indexPath) as? CollectionViewCell else {
            fatalError()
        }
    
        let model = models[indexPath.section]
        cell.userImage.image = UIImage(named: model.imageName)
        cell.userName.text = model.name
        return cell
    }
    }
    
    class CollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var userName: UILabel!
    
    override func prepareForReuse() {
        userImage.image = nil
    }
    }
    

    【讨论】:

    • :你是不是只用了 10 分钟就写完了所有这些? :OOOOOOOOOOOO 非常感谢你。这对我的学习很有帮助!
    • 如果您的问题得到解决,您可以接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多