簡體   English   中英

Self Sizing CollectionView 在 Self Sizing TableViewCell 內

[英]Self Sizing CollectionView inside a Self Sizing TableViewCell

我花了數周時間試圖讓它發揮作用,並在這里、蘋果開發者論壇、谷歌等上查看了許多不同的建議,但我仍在努力解決問題。 任何幫助將不勝感激。

我有一個包含 TableView 的 ViewController。 它不是全屏 tableView。

tableView 是用 customTableViewCells 構建的,每個都有一個 CollectionView。 我遇到的問題是自調整collectionView 和自調整tableView 行似乎不起作用。 我在網上找到了一些選項,但它們似乎只部分有效。

這是我最初運行時得到的:

在此處輸入圖像描述

我包含了項目文件的鏈接,因為這可能比將代碼復制到此處更容易: https://www.dropbox.com/sh/7a2dquvxg62aylt/AACK_TjDxT9eOShZaKi7vLYga?dl=0

一些在線“解決方法”並非在所有情況下都有效 - 例如,如果您點擊 CollectionView 中的一個按鈕,它會被刪除,並且 collectionView(以及隨后的 TableView)應該調整大小,但我還是無法做到這一點始終如一地工作。

我嘗試過的一些解決方案:

UICollectionView 具有自動布局的自定尺寸單元

UITableViewCell 內的 UICollectionView -- 動態高度?

基於動態集合視圖的 UITableView 的動態高度

自動調整包含 UICollectionView 的 UITableViewCell

任何幫助將不勝感激。

我發現出了什么問題。 有幾件事需要修復:

  1. 您需要設置估計的行高(帶有實際值)並將行高設置為自動。 你反其道而行之。 所以請用下面的這個替換你的 setupTableView function。 另外,你需要設置你沒有做的委托。 因此,請確保在 class 名稱旁邊添加 UITableViewDelegate,並像我在下面所做的那樣在 setupTableView() 中分配它。

     func setupTableView() { view.addSubview(tempTableView) NSLayoutConstraint.activate([ tempTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), tempTableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200), tempTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), tempTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200) ]) tempTableView.rowHeight = UITableView.automaticDimension tempTableView.estimatedRowHeight = 90 tempTableView.register(CustomTableViewCell.self, forCellReuseIdentifier: CustomTableViewCell.cellIdentifier) tempTableView.dataSource = self tempTableView.delegate = self tempTableView.translatesAutoresizingMaskIntoConstraints = false tempTableView.layoutIfNeeded() }
  2. 在 cellForRowAt 中,添加 layoutIfNeeded 以確保單元格自動調整大小:

     extension ViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 2 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tempTableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.cellIdentifier) as. CustomTableViewCell cell.layoutIfNeeded() return cell } }
  3. 您的 CustomCollectionView 需要繼承 UICollectionViewDelegateFlowLayout。

  4. 確保將您配置的CollectionViewLayout 變量分配給您的collectionView:

     func setupCollectionView() { backgroundColor =.systemPink isScrollEnabled = false register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: CustomCollectionViewCell.cellIdentifier) dataSource = self self.collectionViewLayout = configuredCollectionViewFlowLayout }
  5. 最后,添加這 3 個覆蓋以使您的 collectionView 根據其內容(在 CustomCollectionView 內)自動調整大小:

     override var intrinsicContentSize: CGSize { self.layoutIfNeeded() return self.contentSize } override var contentSize: CGSize { didSet{ self.invalidateIntrinsicContentSize() } } override func reloadData() { super.reloadData() self.invalidateIntrinsicContentSize() }

這是單擊按鈕時觸發單元格重繪的方式。 我沒有使用協議,但請這樣做,這只是向您展示這個想法:

func tokenTapped(withTitle title: String) {
    guard let indexOfItemToRemove = tokens.sampleData.firstIndex(where: {$0.name == title}) else { return }
    tokens.sampleData.remove(at: indexOfItemToRemove)
    DispatchQueue.main.async {
        self.performBatchUpdates({
            self.deleteItems(at: [IndexPath(item: indexOfItemToRemove, section: 0)])
        }) { (true) in
            if let tableView = self.superview?.superview?.superview as? UITableView {
                print("tableview updates")
                tableView.beginUpdates()
                tableView.endUpdates()
                DispatchQueue.main.async {
                    tableView.layoutIfNeeded()
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM