【问题标题】:How to create collectionview in tableView Cell in swift 4? [duplicate]如何在 swift 4 中的 tableView Cell 中创建 collectionview? [复制]
【发布时间】:2018-04-20 08:54:53
【问题描述】:

]2我用过一个tableView。在tableViewCell 内部,我想填充一个collectionView。 我有一个静态数组

这是我的代码

var menuImage = ["download.jpeg","download (1).jpeg","download (2).jpeg","download (3).jpeg","download (4).jpeg","download (3).jpeg","download (4).jpeg","download (3).jpeg","download (4).jpeg"]


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuImage.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:HomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
        // cell.collectionView.reloadData()
        return cell
    }

tableViewCell内部 ->

class HomeTableViewCell: UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}

这是collectionView

extension HomeVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menuImage.count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
       return  1
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell
        cell.imgvw.image = UIImage(named: menuImage[indexPath.row])
        cell.profileName.text = menus[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
    }
}

仍然没有显示静态数据。请帮忙

【问题讨论】:

  • 你如何设置collectionViewdelegate and dataSource`?
  • 不知道在哪里设置
  • @Kuldeep 请帮忙
  • @Kuldeep 先生你在吗?
  • 是的,告诉我.....

标签: ios swift uitableview uicollectionview swift4


【解决方案1】:

如果您想在用户名下方显示所有图片,则需要更改 Layout。在那种布局中是不可能的。

所以我的建议是:您只在HomeVC 中显示用户详细信息,一旦点击任何行,移动到新屏幕并显示用户详细信息以及所有图像,例如instagram user profile。 p>

编辑您的演示:https://www.dropbox.com/s/v05k2udqa3pu1dd/Demp1.zip?dl=0

【讨论】:

  • 那么我必须在表格视图单元格类中编写 UICollectionViewDelegate、UICollectionViewDataSource 吗?
  • 是的,这是这样做的好方法。
  • 那么应该在 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } 中写什么
  • 只返回您在ViewController中定义的menuImage.count
  • 如果我在视图控制器中定义 menuImage 并在 numberOfRowsInSection 中返回 menuImage.count 那么我应该在 numberOfItemsInSection 中写什么,因为 menuimage 是在 HomeVC 中定义的
【解决方案2】:

我建议您按照视频中提到的步骤Collection View inside TableView cell

此外,您还可以参考此链接: Collectionview in tableview cell

希望这会有所帮助。

已添加图片中的问题

正如您所说,您已将图像直接添加到 Xcode,即拖放,

所以要交叉检查目标成员船是否打勾。?如果不是,请勾选。

还建议正确命名图像,如 image1,image2,image3,image4,etc 以便在没有任何问题的情况下检测到它。

添加了演示项目的编辑链接

您可以在此处的 tableView 单元格中找到 Collection View 的演示:Demo of imageCollectionView inside TableViewCell

【讨论】:

  • 我应该写什么 numberOfRowsInSection?
  • 我会推荐你​​的array.coun
  • 看先生,我们只有一个阵列。菜单图像。我必须在 numberOfItemsInSection 和 numberOfRowsInSection 中计算 menuImage.count?
  • 您是否参考了我发布链接的视频。?因为它清楚地解释了如何在 numberofSections 和集合视图中返回。请检查一次。
  • 虽然我已经看过了..但我又看到了,先生
猜你喜欢
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2018-07-03
相关资源
最近更新 更多