【问题标题】:How to set heightForRowAt based the number of numberOfItemsInSection?如何根据 numberOfItemsInSection 的数量设置 heightForRowAt?
【发布时间】:2017-09-15 02:49:02
【问题描述】:

我正在使用UITableViewController 并将UICollectionView 放在表格视图单元格的第二行。问题是我如何动态获得 heightForRowAt 。

我尝试过使用return UITableViewAutomaticDimension。但不工作。

非常感谢任何帮助。

viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.estimatedRowHeight = self.tableView.rowHeight
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

UITableViewController

选项 1

 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.tableView.bounds.width + 15
    }

选项 2

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

目前,我对退货数量进行了硬编码。

UICollectionViewDataSource

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

UICollectionViewDelegate、UICollectionViewDelegateFlowLayout

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

        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = 2.5
        layout.minimumInteritemSpacing = 2.5

        itemWidth = (collectionView.bounds.width - 5.0) / 3.0
        return CGSize(width: itemWidth, height: itemWidth)
    }

这是使用 return self.tableView.bounds.width + 15 的 UI 图像:

这是使用 return UITableViewAutomaticDimension 的 UI 图像:

这是约束的图像:

【问题讨论】:

  • 你对 UICollectionView 的限制是什么?
  • @TusharSharma 查看更新的问题。全部设置为 0。
  • 您能展示一下您是如何使用自动尺寸的吗?我的意思是您执行了哪些步骤?
  • 我已经更新了问题。目前我正在做的是设置return self.tableView.bounds.width + 15。获得长高。如果我使用UITableViewAutomaticDimension,它将是普通表。
  • @TusharSharma,我已经更新了问题。

标签: ios swift uitableview uicollectionview


【解决方案1】:

在你的 viewDidLoad() 中尝试添加这个 -:

 override func viewDidLoad() {
        super.viewDidLoad()

       yourTableView.estimatedRowHeight = 70 // this is your storyboard default cell height 
       yourtableView.rowHeight = UITableViewAutomaticDimension //Automatic dimensions

    }

然后试试这个-:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

【讨论】:

  • self.tableView.estimatedRowHeight = self.tableView.rowHeight self.tableView.rowHeight = UITableViewAutomaticDimension 我已经实现了。
  • 很抱歉忘记通知您结果。它的发生与图像中的return UITableViewAutomaticDimension 相同。
  • 你有什么建议?
【解决方案2】:

在您的情况下,您必须为具有集合视图的单元格提供动态高度,具体取决于集合视图的内容高度,但是在加载表格视图时,您的集合视图可能不会完全加载那个时候你的集合视图的内容高度可能不正确。

为了获得正确的内容高度,您必须计算集合视图的内容高度,如下所示:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

//Your cell which has a collectionview
    if indexPath.row == 1{ 
    let contentHeight = (numberOfCollectionViewcell*itemHeight)+collectionViewContentOffset
    return contentHeight
    }
            return self.tableView.bounds.width + 15
        }

【讨论】:

  • 我不认为你可以在 UITableView 协议中实现这个collectionViewContentOffset
  • collectionViewContentOffset 是一个固定值,由故事板或代码设置,因此我们可以通过类中任何地方的集合视图的引用来获取它。
【解决方案3】:
  1. 从 CollectionViewDelegate 可以识别collectionview何时完全加载

  2. 获取CollectionView 内容高度。

  3. 通过创建委托将此内容高度发送到tableView VC。

  4. 在委托方法reload tableView中。

  5. tableViewCell CollectionView 中已经重新加载,所以每次执行上述方法时,编译器都会无限地重新加载tableView 和collectionView。为此,您需要添加一个标志(isForcefullyReloadCollectionView),当 tableview 为 tableview 时标志为 true 需要重新加载,否则为 false。您需要在要强制重新加载 tableView 的位置添加此标志。

按照上面的操作,如果您需要进一步的帮助,您可以询问。如果我有任何其他解决方案,我会更新您。

【讨论】:

  • 我应该调用什么来重新加载 tableview ? self.tableView.reloadData ?
  • @Nizzam 是的,但是在您使用 TABLEVIEW 出口的班级中。
  • UICollectionViewDelegate的协议下。在func ....sizeForItemAt.... 我尝试print(collectionView.contentSize.height)。它返回 0。
  • @Nizzam (uicollectionview-has-been-loaded-completely)[stackoverflow.com/questions/14020027/…先让collectionview完全加载,然后检查contentview。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多