简体   繁体   English

如何在tableView(Swift)中删除行后立即选择另一行

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

I am deleting row and want to get next behavior: 我正在删除行,并希望得到下一个行为:

  1. If I am deleting row that was selected, I need to get another row selected automatically. 如果我删除所选的行,我需要自动选择另一行。
  2. And also if I have an additional trouble, when I am perform deleting, target row that is going to be deleted selecting automatically, I don't need it to be selected as I want to only delete it. 而且如果我有一个额外的麻烦,当我执行删除时,将自动选择要删除的目标行,我不需要它被选中,因为我只想删除它。

The main question is 1 for now. 现在的主要问题是1。

I understand that maybe row: 0 does not exists anymore but t's not a point now, I just simplified code to make it more comfortable to discuss the point. 我知道也许row:0不再存在,但现在不是一个点,我只是简化了代码,使讨论这一点变得更加舒适。 I tried performBatchUpdates, and tried to move selectRow(at:section:) to completion closure as well, row selection not works too. 我尝试了performBatchUpdates,并尝试将selectRow(at:section :)移动到完成关闭,行选择也不起作用。

selectRow works perfectly after insertRows function, so I am confused. 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)

    }
}

Ok, I've found solution for the 1st question (just thought maybe such function can exist and typed didrowat , and xcode suggested this didEndEditingRowAt :) 好的,我已经找到第一个问题的解决方案(只是想到也许这样的功能可以存在并输入didrowat ,而xcode建议这个didEndEditingRowAt :)

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

Now first part of question is answered, I've inserted selection there. 现在问题的第一部分已经回答了,我在那里插入了选择。

The second question is still opened, how to make selected row keeping still selected while I am removing another row. 第二个问题仍然是打开的,当我删除另一行时,如何使选定的行保持仍然被选中。 I don't need selected row to deselect at all if I am removing another one. 如果我删除另一个,我根本不需要选择行来取消选择。

As I understood your question, find this solution: 据我了解你的问题,找到这个解决方案:

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)
    }
}

Pass indexPath while deleting a row and after endUpdates, you just need to set the same indexPath as SelectedRow. 在删除行时传递indexPath,在endUpdates之后,只需要将相同的indexPath设置为SelectedRow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM