簡體   English   中英

如何在tableView(Swift)中刪除行后立即選擇另一行

[英]How to select another row right after deleting row in tableView (Swift)

我正在刪除行,並希望得到下一個行為:

  1. 如果我刪除所選的行,我需要自動選擇另一行。
  2. 而且如果我有一個額外的麻煩,當我執行刪除時,將自動選擇要刪除的目標行,我不需要它被選中,因為我只想刪除它。

現在的主要問題是1。

我知道也許row:0不再存在,但現在不是一個點,我只是簡化了代碼,使討論這一點變得更加舒適。 我嘗試了performBatchUpdates,並嘗試將selectRow(at:section :)移動到完成關閉,行選擇也不起作用。

selectRow在insertRows函數后完美運行,所以我很困惑。

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: .fade)
        tableView.endUpdates()

        tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .middle)

    }
}

好的,我已經找到第一個問題的解決方案(只是想到也許這樣的功能可以存在並輸入didrowat ,而xcode建議這個didEndEditingRowAt :)

override func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
    tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .middle)
}

現在問題的第一部分已經回答了,我在那里插入了選擇。

第二個問題仍然是打開的,當我刪除另一行時,如何使選定的行保持仍然被選中。 如果我刪除另一個,我根本不需要選擇行來取消選擇。

據我了解你的問題,找到這個解決方案:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: .fade)
        tableView.endUpdates()
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    }
}

在刪除行時傳遞indexPath,在endUpdates之后,只需要將相同的indexPath設置為SelectedRow。

暫無
暫無

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

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