簡體   English   中英

選擇時展開 tableviewcell,取消選擇時壓縮 Swift 3

[英]Expand tableviewcell when selected and compact when deselected Swift 3

所以我有一個類型為 ExpyTableView ( https://github.com/okhanokbay/ExpyTableView ) 的 mainTableView 。 一切都很好,我設法實現了它並使其工作,但正如在下面的 gif 中看到的那樣,我需要為 DidSelect 和 DidDeselect 進行 +1 額外操作。

演示 gif

問題是我想要在被選中時用綠色突出顯示並立即展開其他行,然后在單擊它之后立即取消選擇並使該行恢復正常。 所以通常這需要在屏幕上點擊 2 次后發生......相反,我在 gif 中看到了 4 次。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //MARK : Print
    print("Selected Section \(indexPath.section), Row : \(indexPath.row)")
    ///////////////
   
    if let cell = tableView.cellForRow(at: indexPath) {
        if (indexPath.section == 1) || (indexPath.section == 2) || (indexPath.section == 3) {
        cell.layer.borderWidth = 2.0
        cell.layer.borderColor = Theme.defaultColor().cgColor
        cell.layer.shadowOffset = CGSize.init(width: 0.5, height: 0.5)
        }
    }
}

   func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        cell.layer.borderWidth = 0.1
        cell.layer.borderColor = UIColor.lightGray.cgColor
        
    }
}

我哪里做錯了?

基本上,您會聽到didDeselectRow表更改,這意味着:一旦取消選擇表視圖單元格,就會執行代碼。 所以你可以忘記那個代碼,除非你真的需要它。 但是,您所做的是兩次selects該行。 一是擴張,二是崩潰。

您需要做的是記住所選單元格的索引,然后在 id didSelectRowAt取消選擇它,因此聲明一個屬性fileprivate var currentlySelectedCellIndexPath: IndexPath? 然后在didSelectRowAt使用以下內容。

var currentlySelectedCellIndexPath: IndexPath?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let currentlySelectedCellIndexPath = currentlySelectedCellIndexPath {
        // unselect the selected one
        makeCellUnSelected(in: tableView, on: currentlySelectedCellIndexPath)
        guard currentlySelectedCellIndexPath != indexPath  else {
            tableView.deselectRow(at: currentlySelectedCellIndexPath , animated: true)
            self.currentlySelectedCellIndexPath = nil
            return
        }
    }

    // Highlight the proper cell
    makeCellSelected(in: tableView, on: indexPath)
    currentlySelectedCellIndexPath = indexPath
}

func makeCellSelected(in tableView: UITableView, on indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        if (indexPath.section == 1) || (indexPath.section == 2) || (indexPath.section == 3) {
            cell.layer.borderWidth = 2.0
            cell.layer.borderColor = Theme.defaultColor().cgColor
            cell.layer.shadowOffset = CGSize.init(width: 0.5, height: 0.5)
        }
    }
}

func makeCellUnSelected(in tableView: UITableView, on indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.layer.borderWidth = 0.1
        cell.layer.borderColor = UIColor.lightGray.cgColor
    }
}

解釋它的作用:函數 makeCellselected 和 makeCellUnselected 只是應用樣式更改來突出顯示/取消突出顯示單元格,就像您之前所做的那樣。

在函數func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)只需檢查當前可見和選定的單元格。 如果它是同一個單元格,則將其折疊為守衛並返回,因為您只需要展開/折疊。 如果它不是同一個單元格,則只需通過調用makeCellUnselected丟棄選擇並選擇新的單元格。 到目前為止,這應該可以解決您的問題。 如果您有任何其他問題,請告訴我。

我會做的是在您的模型中有一個屬性isExpanded將存儲單元格是否需要擴展。 這樣您就可以在heightForRowAtIndexPath中使用它,您將在其中獲取項目並讀取isExpanded屬性。

暫無
暫無

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

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