【问题标题】:How to change the cell height after putting a collection view into a table view将集合视图放入表格视图后如何更改单元格高度
【发布时间】:2018-03-28 17:53:53
【问题描述】:

我正在尝试将一些水平滚动的图像放入 tableview 单元格中,所以我使用了一个 collectionview,我将它放入了单元格中,除了单元格高度之外,一切正常。我已经在故事板中选择了要自定义的单元格高度,并且也使用代码更改了单元格高度,但它仍然不起作用。我是菜鸟,找不到问题出在哪里,请大家帮帮我,谢谢! 1.I want to change the cell height 2.Stroyboard

class HomePageTableViewController: UITableViewController {

var ImageArray = [String]()

override func viewDidLoad() {

    super.viewDidLoad()

    ImageArray = ["Calendar.png","Calendar.png","Calendar.png","Calendar.png","Calendar.png"]

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return ImageArray.count

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "BannerCell") as! BannerTableViewCell

    return cell
}

override func viewWillAppear(_ animated: Bool) {
    tableView.estimatedRowHeight = 100
    tableView.rowHeight = UITableViewAutomaticDimension
  }   
}
   extension HomePageTableViewController: UICollectionViewDelegate, 
   UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1

}

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

    return ImageArray.count

}

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BannerCollection", for: indexPath) as! BannerCollectionCollectionViewCell

        cell.BannerImage.image = UIImage(named: ImageArray[indexPath.row])

        return cell


     }
 }

【问题讨论】:

  • TableViews 有一个名为heightForRowAt: IndexPath 的函数调用该函数并在其中设置高度。如果集合中有多个单元格,CollectionViews 有一个称为 flowLayout 的东西。
  • 你的代码中没有单元格,它是一个单独的tableView和collectionView
  • 您的收藏视图在哪里?您是否将其添加到 BannerCell 自定义表格视图单元格中?您在哪里为您的 collectionview 设置了数据源和委托?
  • @Shawn 我创建了两个名为 BannerTableViewCell 和 BannerCollectionViewCell 的类,并将表格视图单元格标识符设置为“BannerCell”,并且在情节提要中集合可重用视图标识符为“BannerCollection”,数据源和委托已连接到表格视图控制器。

标签: ios iphone swift xcode swift4


【解决方案1】:

您可能需要设置 collectionviewcell 的大小,如下所示:

extension HomePageTableViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        // Return the cell size
        let cellSize = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.width * (ratioToUse))
        return cellSize
    }
}

我在使用collectionview 水平滚动时做了类似的事情,但我使用每个单元格的全宽并计算纵横比。但是,您需要将其修改为您想要的大小。

我不得不在其他地方做一些技巧来调整collectionview本身的大小。

然后我会先尝试一下,如果您仍然遇到问题,请发布它的外观图片。

更新 #2:

如果您使用的是 AutoLayout,则不需要这样做,但您可以使用以下方法自行设置高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    <#code#>
}

更新#3:

我只是对此进行了一些测试。你应该对你的collectionview设置一个高度约束,这应该使tableviewcell适当地调整大小。正如我上面提到的,你不应该需要“he​​ightForRowAt”。 AutoLayout 应该会为您处理这个问题。

【讨论】:

  • 首先感谢您的帮助!? 但是我要更改的是tableview单元格,而不是collectionview,我尝试了您的代码,它改变了我的collectionview单元格,tableview单元格没有改变。跨度>
  • 您在使用自动布局吗?通常,我使用自动布局并指定 tableView.rowHeight = UITableViewAutomaticDimension / tableView.estimatedRowHeight = XXX。如果您的约束是正确的,tableviewcell 的大小应该正确。
  • 是的,我在collectionview和imageview中使用了Autolayout,也使用了那个代码,但是还是没有?。
  • @JesseS,你能展示一下你得到的截图吗?
  • 我已将图片链接放入问题中,再次感谢您的帮助!?
【解决方案2】:

尝试将 UIImageView 的内容模式更改为宽高比。它可能会起作用。

【讨论】: