ios - Swift:根据单元格数量动态更改单元格高度

标签 ios swift uitableview uicollectionview

我有一个 UICollectionView 网格系统,其中 indexPath.item % 3 == 0 ? largeCellSize : smallCellSize 并且有效。 This是它的外观截图。 UICollectionView 位于 UITableViewCell 的单元格内,UITableView 位于 ViewController 内。

UITableViewCell 高度由 UICollectionView 中的单元格数量决定。因此,当 largeCellSize 可用时,单元格高度应该增加,因为 smallCellSize 跟在 largeCellSize 之后。

例如:大、小、小 -> 增加单元格高度 -> 大、小、小 -> 增加单元格高度 -> 等等......真的没有倍数,因为它是大细胞出现在第 1、4、7、10、13、16 等...

largeCellSize 的大小为 400smallCellSize 的大小为 200。所以基本上,UITableViewCell 大小应该只增加 400 当有这个 1st, 4th, 7th, 10th, 13th, 16th, etc... "pattern"单元格数。

我如何通过确保单元格高度在适当的时候增加 400 来解决这个问题?

代码:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch indexPath.row {
    case 0: // Ignore this...
        return 110
    case 1: // Problem is here...
        let productCellCount = self.productsArray.count

        print("---------- heighForRowAt")
        print(productCellCount)

        // I can't seem to get the right logic to show just until the last cell.
        // This shows the last cell and then a bunch of empty space.
        // As for the false, it shows 6 out of the 11 cell and
        // the large cell that it's supposed to show it shows only half of it

        return CGFloat(productCellCount % 3 == 0 ? Double(400 * productCellCount / 2) : 200 * ceil(Double(productCellCount / 2)))
    default:
        return UITableViewAutomaticDimension
    }
}

更新:

澄清一下,我的 UITableView 中有两个单元格,第二个单元格包含正在填充来自 Firebase 的数据的 UICollectionView。随着数据的增加/减少,细胞的数量显然会发生变化。根据单元格的数量,UITableViewCell 的高度将增加/减少。

最佳答案

实现 collectionView(_:layout:sizeForItemAt:) UICollectionViewDelegateFlowLayout方法。将您需要的有关 indexPath 逻辑的条件放在那里,每次重新加载表格时它都应该正确呈现

关于ios - Swift:根据单元格数量动态更改单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46724941/

相关文章:

ios - xcode for iphone 中的设置包

ios - 使用 mailcore2 在 gmail 帐户中创建标签

ios - 在 OCMPartialMock 上 stub 类方法?

iphone - iOS 中的 LinkedIn 集成 : Invitation API not working

ios - 我想使用两个按钮选择图像,并希望在 swift 中的两个不同 ImageView 上显示这些图像。我该怎么做?

swift - 如何在 sqlite 或 CoreData 中保存位置坐标,并在 swift 中进行重要的位置监控

ios - 从一个 View Controller 中触发另一个 View Controller 中的特定操作

ios - ViewController添加UITableView使用scrollViewDidScroll popViewControllerAnimated崩溃

ios - 如何使用swift更新tableview中的数据?

ios - UIView 卡在 UITableview header 后面