簡體   English   中英

在每個單元格都包含表格視圖的表格視圖中選擇新單元格時,無法取消選擇先前選擇的單元格

[英]Can’t deselect previously selected cell when a new one is selected in a tableview where each cell contains tableview

我有一個表格視圖,其中每個單元格都是一個自定義表格視圖單元格。 該自定義表格視圖單元格包含一個表格視圖和一個 label。 可以說,外部 tableview 稱為 MainTableView。 MainTableView 的每個單元格都包含另一個 tableview。 問題是,當我 select 內部 tableview 單元格一個接一個時,先前選擇的單元格沒有被取消選擇。在第一張圖像中,我選擇的單元格包含文本“Two”。 然后在第二張圖片中,我選擇的單元格包含文本“五”,但之前選擇的單元格“二”仍處於選擇模式。 我想在 select 一個新的單元格時取消選擇上一個單元格。

第一張圖片 第二張圖片

我努力了

tableView.deselectRow(at: IndexPath, animated: Bool)

此方法在自定義 tableviewcell class 中的 didSelectRowAt 中,但它沒有達到目的,因為先前的 indexPath 來自單獨的 tableview。 那么,如何取消選擇上一個呢?

獲得正確的內部tableView,

首先,您應該記錄外部 tableView 的單元格 indexPath,內部 tableView 所在的單元格。

所以你應該記錄兩個 indexPathes

var selectionRowInfo: (lastOutsideIP: IndexPath?, lastInnerIP: IndexPath?)

其次,您通過outsideTableView 獲得正確的tableView。

如果內表可見,則應立即處理。 通過outTableView.indexPathsForVisibleRows

else 條件,你不需要處理它。 tableView重用機制會刷新它的state。

    // pseudo code.


    if let lastOut = lastOutsideIP, let visibleRows = outTableView.indexPathsForVisibleRows, visibleRows.contains(lastOut){
        let cell = tableView.cellForRow(at: lastOut) as! YourCell
        // get the correct inner tableView via the cell
    }

因為內部 tableViews 彼此不相關。 Select 表一的單元格,不會影響表二的單元格的選擇。

所以你應該手動建立連接。

使用屬性存儲 state var lastIndexPath: IndexPath? ,

然后每次 select 一個 indexPath,

  // pseudo code.
  if let last = lastIndexPath{
        tableView.deselectRow(at: last, animated: true) 
  }

請注意,您應該找到正確的內部 tableView,它具有lastIndexPath

上一個答案是正確的,但有一個缺陷 - 它無法區分 tableViews,這是最重要的部分。 此外,如果 tableViews 具有不同數量的行,則可能會嘗試訪問不存在的行並導致崩潰。

要在兩個 tableViews ( tv1 & tv2 ) 中跟蹤選定的行,您需要在每個表中保留選定的行:

var tv1, tv2: UITableView!
var lastRowForTV1, lastRowForTV2: IndexPath?

然后通過識別正在使用的 tableView 並調整另一個來響應選擇(這假設兩個 tableViews 使用相同的數據源/委托)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      if tableView === tv1 {
         lastRowForTV1 = indexPath
         if let last = lastRowForTV2 {
            tv2.deselectRow(at: last, animated: true)
            lastRowForTV2 = nil
         }
      } else if tableView === tv2 {
         lastRowForTV2 = indexPath
         if let last = lastRowForTV1 {
            tv1.deselectRow(at: last, animated: true)
            lastRowForTV1 = nil
         }
      }
   }

我通過使用dengApro給出的第一個答案的想法解決了這個問題。 這個想法是找到包含先前選擇的單元格的正確內表。 我有兩個文件,一個是 ViewController.swift,其中包含外部 tableview MainTableView。 另一個是 CustomTableViewCell.swift 和 CustomTableViewCell.xib,其中包含帶有表格視圖的自定義單元格。

fileprivate var lastSelectedInnerTableView : Int = -1
fileprivate var lastSelectedRow: Int = -1
fileprivate var tableViewList :[UITableView] = []

我在 class CustomTableViewCell 之外的 CustomTableViewCell.swift 文件中添加了這 3 個變量。 lastSelectedSection、lastSelectedRow 這兩個變量用於跟蹤最后選擇的內部表格視圖(lastSelectedInnerTableView)和該內部表格視圖的最后選擇的單元格(lastSelectedRow)。 tableViewList 變量用於保存 Inner tableView。 在 awakeFromNib() 里面我有 append 創建的內部表視圖。

override func awakeFromNib() {
    super.awakeFromNib()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    tableViewList.append(self.tableView) // append the created inner tableviews
}

然后在 didSelectRowAt 里面我取消了前一個:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if lastSelectedInnerTableView != -1 && lastSelectedRow != -1{
        self.oldIndexPath.section = 0
        self.oldIndexPath.row = lastSelectedRow
        tableViewList[lastSelectedInnerTableView].deselectRow(at: self.oldIndexPath, animated: true)
    }

    lastSelectedInnerTableView = self.innerTableId
    lastSelectedRow = indexPath.row
}

在 ViewController.swift 我在 cellForRowAt 中設置了 innerTableId

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let customCell: CustomTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomTableViewCell
    customCell.innerTableId = indexPath.row
    customCell.customCellActionDelegate = self

    return customCell
}

暫無
暫無

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

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