viewWillAppear 不调用 didDeselectRowAt

viewWillAppear not calling didDeselectRowAt

我目前有一个 tableView,我为其创建了自定义选择和取消选择操作(淡入和淡出视图)。我遇到了一个问题,即在返回 tableView 时未调用取消选择操作。我已将必要的取消选择代码添加到我的 viewWillAppear 中,因此似乎无法弄清楚可能出了什么问题。这个用例有不同的方法吗?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Deselect row on unwind
    if let path = folderTableView.indexPathForSelectedRow {
        folderTableView.deselectRow(at: path, animated: true)
    }
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {
    print("Select")

    switch indexPath.section {
    case 2:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
        cell.folderTagSelectionBKG.alpha = 1.0
    default:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
        cell.folderSelectionBKG.alpha = 1.0
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)  {
    print("Should deselect")

    switch indexPath.section {
    case 2:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
            cell.folderTagSelectionBKG.alpha = 0
        })
    default:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
            cell.folderSelectionBKG.alpha = 0
        })
    }
}

如果您使用 UITableViewController 子类,则只需设置 属性


self.clearsSelectionOnViewWillAppear = YES;

其他在 viewDidAppear 上调用

NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
if (indexPath) {
    [self.tableView deselectRowAtIndexPath:indexPath animated:animated];
}

// MARK: - Swift 3
if let indexPath = tableView.indexPathForSelectedRow {
    tableView.deselectRow(at: indexPath, animated: true)
}

How to unselect a uitableview cell when the user returns to the view controller

从您的 tableView 展开操作中,尝试放置您的取消选择代码。像这样:

@IBAction func <UnwindName>(segue : UIStoryboardSegue) {

    if let indexPath = tableView.indexPathForSelectedRow {
       tableView.deselectRow(at: indexPath, animated: true)
    }
}

来自 deselectRow(at:animated:)

的文档

Calling this method does not cause the delegate to receive a tableView(_:willDeselectRowAt:) or tableView(_:didDeselectRowAt:) message, nor does it send selectionDidChangeNotification notifications to observers.

Calling this method does not cause any scrolling to the deselected row.

一个解决方案是将 didDeselectRowAt 中的代码移动到一个额外的方法中

func deselectRowAnimated(at indexPath : IndexPath)
{
    switch indexPath.section {
    case 2:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
            cell.folderTagSelectionBKG.alpha = 0
        })
    default:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
            cell.folderSelectionBKG.alpha = 0
        })
    }
}

并在 viewWillAppeardidDeleselect

中调用它
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Deselect row on unwind
    if let indexPath = folderTableView.indexPathForSelectedRow {
        deselectRowAnimated(at: indexPath)
    }
}

...

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)  {
    deselectRowAnimated(at: indexPath)
}

选择行